Answered by:
C# Application : modify a cs configuration file

Question
-
Hello,
In my C# application I have a cs file which containing my ftp parameters :
class FtpConfig { public string ServerAdress = "10.10.100.100"; public string UserName = UserName = "usertest"; public string PathFolder = PathFolder = "/test/testfolder"; public string Password = Password = "aaaaa"; }
But in my app, I need to edit the configuration of the ftp (if it changes).
How can I edit this file and save it as the new configuration file ?
Ty !
- Edited by gauthiermt Monday, March 16, 2020 4:37 PM
Monday, March 16, 2020 4:36 PM
Answers
-
Hi gauthiermt,
You can use App.config file in you app to holding your FTP information and encrypt sensitive data like User Name and Password I have added save as dialog to save as text file.
hope this way help you.app.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="ServerAdress" value="0.10.100.100" /> <add key="UserName" value="usertest" /> <add key="PathFolder" value="/test/testfolder" /> <add key="Password" value="aaaaa" /> </appSettings> </configuration>
C# code in a windows from
using System; using System.Windows.Forms; using System.Configuration; using System.IO; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); txtserver.Text = ConfigurationManager.AppSettings["ServerAdress"]; txtusername.Text = ConfigurationManager.AppSettings["UserName"]; txtpathfolder.Text = ConfigurationManager.AppSettings["PathFolder"]; txtpassword.Text = ConfigurationManager.AppSettings["Password"]; } private void btnsave_Click(object sender, EventArgs e) { Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); config.AppSettings.Settings["ServerAdress"].Value = txtserver.Text; config.AppSettings.Settings["UserName"].Value = txtusername.Text; config.AppSettings.Settings["PathFolder"].Value = txtpathfolder.Text; config.AppSettings.Settings["Password"].Value = txtpassword.Text; config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); txtserver.Text = ConfigurationManager.AppSettings["ServerAdress"]; txtusername.Text = ConfigurationManager.AppSettings["UserName"]; txtpathfolder.Text = ConfigurationManager.AppSettings["PathFolder"]; txtpassword.Text = ConfigurationManager.AppSettings["Password"]; } private void btnsaveas_Click(object sender, EventArgs e) { SaveFileDialog save = new SaveFileDialog(); save.FileName = "ftp.txt"; save.Filter = "Text File | *.txt"; if (save.ShowDialog() == DialogResult.OK) { StreamWriter writer = new StreamWriter(save.OpenFile()); foreach (string key in ConfigurationManager.AppSettings) { string value = ConfigurationManager.AppSettings[key]; writer.WriteLine("{0} : {1}", key, value); } writer.Dispose(); writer.Close(); } } } }
Please remember to mark the replies as answers if they helped you :) ~
- Marked as answer by gauthiermt Tuesday, March 17, 2020 1:46 PM
Monday, March 16, 2020 7:35 PM
All replies
-
Hi gauthiermt,
You can use App.config file in you app to holding your FTP information and encrypt sensitive data like User Name and Password I have added save as dialog to save as text file.
hope this way help you.app.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="ServerAdress" value="0.10.100.100" /> <add key="UserName" value="usertest" /> <add key="PathFolder" value="/test/testfolder" /> <add key="Password" value="aaaaa" /> </appSettings> </configuration>
C# code in a windows from
using System; using System.Windows.Forms; using System.Configuration; using System.IO; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); txtserver.Text = ConfigurationManager.AppSettings["ServerAdress"]; txtusername.Text = ConfigurationManager.AppSettings["UserName"]; txtpathfolder.Text = ConfigurationManager.AppSettings["PathFolder"]; txtpassword.Text = ConfigurationManager.AppSettings["Password"]; } private void btnsave_Click(object sender, EventArgs e) { Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); config.AppSettings.Settings["ServerAdress"].Value = txtserver.Text; config.AppSettings.Settings["UserName"].Value = txtusername.Text; config.AppSettings.Settings["PathFolder"].Value = txtpathfolder.Text; config.AppSettings.Settings["Password"].Value = txtpassword.Text; config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); txtserver.Text = ConfigurationManager.AppSettings["ServerAdress"]; txtusername.Text = ConfigurationManager.AppSettings["UserName"]; txtpathfolder.Text = ConfigurationManager.AppSettings["PathFolder"]; txtpassword.Text = ConfigurationManager.AppSettings["Password"]; } private void btnsaveas_Click(object sender, EventArgs e) { SaveFileDialog save = new SaveFileDialog(); save.FileName = "ftp.txt"; save.Filter = "Text File | *.txt"; if (save.ShowDialog() == DialogResult.OK) { StreamWriter writer = new StreamWriter(save.OpenFile()); foreach (string key in ConfigurationManager.AppSettings) { string value = ConfigurationManager.AppSettings[key]; writer.WriteLine("{0} : {1}", key, value); } writer.Dispose(); writer.Close(); } } } }
Please remember to mark the replies as answers if they helped you :) ~
- Marked as answer by gauthiermt Tuesday, March 17, 2020 1:46 PM
Monday, March 16, 2020 7:35 PM -
You should learn how to use a class with public auto properties.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties
You can then XML serialize the object saving it to a file.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/serialization/how-to-read-object-data-from-an-xml-file
You can make a change to the data in the XML file that represents the class/object.
You can read the XML file and deserialize it back to an object.
Monday, March 16, 2020 9:15 PM