BitServer.INI.setSetting C# (CSharp) Method

setSetting() public static method

Sets a single Value. Prevents Comments from being overwritten.
public static setSetting ( string FileName, string Section, string Setting, string Value ) : void
FileName string File Name
Section string INI Section
Setting string INI Setting
Value string Value to set
return void
        public static void setSetting(string FileName, string Section, string Setting,string Value)
        {
            if (!string.IsNullOrEmpty(FileName) && File.Exists(FileName))
            {
                if (!string.IsNullOrEmpty(Section) && !string.IsNullOrEmpty(Setting))
                {
                    var Lines = File.ReadAllLines(FileName);
                    var inSect = false;

                    for (int i = 0; i < Lines.Length; i++)
                    {
                        if (inSect)
                        {
                            if (Lines[i].ToLower().StartsWith(Setting.ToLower() + "="))
                            {
                                Lines[i] = Lines[i].Split('=')[0] + "=" + Program.toEmpty(Value);
                                break;
                            }
                            else if (Lines[i].StartsWith("[") && Lines[i].EndsWith("]"))
                            {
                                inSect = false;
                            }
                        }
                        else
                        {
                            inSect = (Lines[i].ToLower().Trim() == "[" + Section.ToLower() + "]");
                        }
                    }
                    File.WriteAllLines(FileName, Lines);
                }
            }
            else
            {
                File.WriteAllText(FileName,string.Format("[{0}]\r\n{1}={2}",Section,Setting,Value));
            }
        }

Usage Example

Example #1
0
        internal static void storeBS(BitSettings B)
        {
            INI.setSetting(CONFIG, "API", "FILE", B.BitConfig);
            INI.setSetting(CONFIG, "API", "DEST", B.IP);
            INI.setSetting(CONFIG, "API", "PORT", B.Port.ToString());
            INI.setSetting(CONFIG, "API", "NAME", B.UName);
            INI.setSetting(CONFIG, "API", "PASS", B.UPass);
            INI.setSetting(CONFIG, "MAIL", "RANDOM", B.Random?"TRUE":"FALSE");
            INI.setSetting(CONFIG, "MAIL", "STRIP", B.StripHdr ? "TRUE" : "FALSE");
            INI.setSetting(CONFIG, "MAIL", "RQUOT", B.RemQuoting? "TRUE" : "FALSE");
            INI.setSetting(CONFIG, "MAIL", "EXT", B.Extension);
            initBS();
            BitAPIserver.init(BS);
            bool passIO = true;

            try
            {
                if (BitAPIserver.BA.helloWorld("A", "B") != "A-B")
                {
                    passIO = false;
                    throw new Exception("Wrong Password");
                }
            }
            catch
            {
                string MSG = "Cannot reach the Bitmessage API.";
                if (!passIO)
                {
                    MSG = "Username and Password seem incorrect.";
                }
                if (MessageBox.Show(MSG + "\r\nPlease double check your Settings.\r\nDo so now?",
                                    "BitAPI not reached", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
                {
                    Settings_Click(null, null);
                }
            }
        }