IniParser.FileIniDataParser.WriteFile C# (CSharp) Method

WriteFile() public method

Writes INI data to a text file.
public WriteFile ( string filePath, IniParser.IniData parsedData, Encoding fileEncoding = null ) : void
filePath string /// Path to the file. ///
parsedData IniParser.IniData /// IniData to be saved as an INI file. ///
fileEncoding Encoding /// Specifies the encoding used to create the file. ///
return void
        public void WriteFile(string filePath, IniData parsedData, Encoding fileEncoding = null)
        {
            // The default value can't be assigned as a default parameter value because it is not
            // a constant expression.
            if (fileEncoding == null)
                fileEncoding = Encoding.ASCII;

            if (string.IsNullOrEmpty(filePath))
                throw new ArgumentException("Bad filename.");

            if (parsedData == null)
                throw new ArgumentNullException("parsedData");

            using (FileStream fs = File.Open(filePath, FileMode.Create, FileAccess.Write))
            {
                using (StreamWriter sr = new StreamWriter(fs, fileEncoding))
                {
                    WriteData(sr, parsedData);
                }
            }
        }

Usage Example

        //constructor
        public ConfigHandler()
        {            
            FileIniDataParser Parser = new FileIniDataParser();

            string configDir = String.Format(@"{0}\config", GetLocalDir());
            string configPath = String.Format(@"{0}\config\launcherConfig.ini", GetLocalDir());

            //release 0.0.
            string defaultLauncherVersion = "0.0.3";

            if (!Directory.Exists(configDir))
            {
                Directory.CreateDirectory(configDir);
            }
            if (!File.Exists(configPath))
            {
                //here we create a new empty file
                FileStream configStream = File.Create(configPath);
                configStream.Close();

                //read the file as an INI file
                try
                {
                    IniData data = Parser.ReadFile(configPath);

                    data.Sections.AddSection("Local");
                    data.Sections.AddSection("Remote");
                    data.Sections.AddSection("Launchpad");

                    data["Local"].AddKey("launcherVersion", defaultLauncherVersion);
                    data["Local"].AddKey("gameName", "Example");
                    data["Local"].AddKey("systemTarget", "Win64");

                    data["Remote"].AddKey("FTPUsername", "anonymous");
                    data["Remote"].AddKey("FTPPassword", "anonymous");
                    data["Remote"].AddKey("FTPUrl", "ftp://example.example.com");

                    data["Launchpad"].AddKey("bOfficialUpdates", "true");

                    Parser.WriteFile(configPath, data);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.StackTrace);
                }
                
            }
            else 
            {
                IniData data = Parser.ReadFile(configPath);
                data["Local"]["launcherVersion"] = defaultLauncherVersion;

                Parser.WriteFile(configPath, data);
            }
        }
All Usage Examples Of IniParser.FileIniDataParser::WriteFile