BitServer.INI.RewriteINI C# (CSharp) Method

RewriteINI() public static method

Rewrites a complete INI File. This erases Comments.
public static RewriteINI ( string FileName, INIPart Parts ) : void
FileName string File Name
Parts INIPart INI Sections (aka. Parts)
return void
        public static void RewriteINI(string FileName, INIPart[] Parts)
        {
            if (Parts != null && Parts.Length>0)
            {
                var Content = string.Empty;
                foreach (INIPart p in Parts)
                {
                    Content += string.Format("[{0}]\r\n",p.Section);
                    foreach (var k in p.Settings.AllKeys)
                    {
                        Content += string.Format("{0}={1}\r\n", k, Program.toEmpty(p.Settings[k]));
                    }
                    Content += "\r\n";
                }
                File.Delete(FileName);
                File.WriteAllText(FileName, Content.Trim());
            }
        }

Usage Example

Example #1
0
        static void Main(string[] args)
        {
            if (initBS())
            {
                //Check First Run (-1)
                if (BS.Port != -1 && !BitAPIserver.init(BS))
                {
                    MessageBox.Show(@"Error reading API values.
Check if keys.dat is present and API values are set.
Also verify the API File Path in BitServer.ini is valid.", "Initialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    AuxMessages = new List <POP3message>();
                    AckMessage  = new List <string>();

                    tDSN = new Thread(new ThreadStart(checkMSG));
                    tDSN.IsBackground = true;
                    tDSN.Start();

                    if (BS.Port == -1)
                    {
                        Settings_Click(null, null);
                        Application.Run(GUI = new frmLoop());
                    }
                    else
                    {
                        Application.Run(GUI = new frmLoop());
                    }
                }
            }
            else
            {
                var Parts = new INI.INIPart[] { new INI.INIPart(), new INI.INIPart() };
                var NVC   = new System.Collections.Specialized.NameValueCollection();

                Parts[0].Section = "API";
                NVC.Add("FILE", "keys.dat");
                NVC.Add("NAME", "AyrA");
                NVC.Add("PASS", "BitMailServer");
                NVC.Add("DEST", "127.0.0.1");
                NVC.Add("PORT", "-1");
                Parts[0].Settings = NVC;

                Parts[1].Section = "MAIL";
                NVC = new System.Collections.Specialized.NameValueCollection();
                NVC.Add("RANDOM", "TRUE");
                NVC.Add("STRIP", "TRUE");
                Parts[1].Settings = NVC;

                INI.RewriteINI(CONFIG, Parts);

                MessageBox.Show("Error reading BitServer.ini Values.\r\nA File with Default Settings was created.\r\nPlease change its settings in the folowing Window.", "Initialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Restart();
            }

            cont = false;

            Console.WriteLine("Exiting...");
            if (NFI != null)
            {
                NFI.Visible = false;
                NFI.Dispose();
                CMS.Dispose();
            }
            tDSN.Join();
        }