Skip to content

Instantly share code, notes, and snippets.

@cosminpopescu14
Last active December 17, 2017 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosminpopescu14/6c94142e271f6300dc6d776f27c469c4 to your computer and use it in GitHub Desktop.
Save cosminpopescu14/6c94142e271f6300dc6d776f27c469c4 to your computer and use it in GitHub Desktop.
Working with app.config in c#
//first, let's instantiate the Configuration manager class
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (ConfigurationManager.AppSettings.Count == 0)
{
configuration.AppSettings.Settings.Add("ffmpegPath", "");
configuration.Save(ConfigurationSaveMode.Full);
ConfigurationManager.RefreshSection("appSettings");
}
string key = ConfigurationManager.AppSettings["ffmpegPath"];
foreach (string key in ConfigurationManager.AppSettings)
{
string value = ConfigurationManager.AppSettings[key];
if (value == "")
{
MessageBox.Show("ffpeg path not configured !");
openFileDialog1.Title = "Select ffmpeg executable file";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string ffmpegPath = openFileDialog1.FileName;
//Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//configuration.AppSettings.Settings.Add("ffmpegPath", Path.GetFullPath(ffmpegPath));
var settings = configuration.AppSettings.Settings;
settings["ffmpegPath"].Value = Path.GetFullPath(ffmpegPath);
configuration.Save(ConfigurationSaveMode.Full, true);
ConfigurationManager.RefreshSection("appSettings");
}
}
else
{
MessageBox.Show("ffmpeg path already configured !");
}
}
@cosminpopescu14
Copy link
Author

Sorry for code style !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment