I have a customized file configFile , and I need to read out the Property.Settings from the applicationConfig section in the file.
the configuration can be loaded in this way:
var localConfig = System.Configuration.ConfigurationManager.OpenExeConfiguration(configFile);
I can find localConfig.AppSettings, but not localConfig.ApplicationSettings.
Is there a way to do so? thanks.
Related
What is the best way to use a config file in a UWP application?
Store some string values in a config file that can be edited without that the application need to be rebuild everytime.
Somthing like a web.config file.
If you intend to side-load the app, you could add a text or XML file to it (with the Build Action property set to Content) that contains some pre-defined configuration values and then read from this file at runtime. e.g.:
var sampleFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("sample.txt");
var contents = await Windows.Storage.FileIO.ReadTextAsync(sampleFile);
I have a list box and that list box contain multiple string value. I want to add those strings to config file and want to read values from config and put it in list box. what should be the approach? I am new to configuration file.
The following snippet will help you modify the app.config file of your project.
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
config.AppSettings.Settings.Remove("MySetting");
config.AppSettings.Settings.Add("MySetting", "some value");
config.Save(ConfigurationSaveMode.Modified);
For reading the config values, you can simply use ConfigurationManager.AppSettings["MySetting"]
You can store and retrieve configuration data for your app via it's app.config file.
You'll need to add a reference to System.Configuration in your code and add the System.Configuration assembly to your application.
Here is a good post with more info - What is App.config in C#.NET? How to use it?
probably you wanted to have those values present in a file and then read from that file and attach to your listbox like
string path = #"c:\temp\MyTest.txt";
if (File.Exists(path))
{
string[] readText = File.ReadAllLines(path);
}
You can attach that your listbox
listbox1.DataSource = new List<string>().AddRange(readText);
I want to get current config file from C#
Then I was found code to get current log file from this code
var rootAppender = ((Hierarchy)LogManager.GetRepository()).Root
.Appenders.OfType<FileAppender>().FirstOrDefault();
string filename = rootAppender != null ? rootAppender.File : string.Empty;
So It return log Path but I want current in used config file (ex. C:\TestLog\WriteLog.log4net)
Thank you
A quick look at the source code suggests this isn't possible, but if you're prepared to do a custom build of log4net it looks straightforward, maybe add the path to ILoggerRepository and populate it in InternalConfigure(ILoggerRepository repository, FileInfo configFile)
I have a WPF application. This application should read a web.config file (from a asp.net webforms project) and a myCompany.config file.
How can I do this? Is there a easy way to handle this?
Any help would be appreciated.
thanks!
Sounds like you need to use configurationmanager
http://msdn.microsoft.com/en-us/library/aa719887%28v=vs.71%29.aspx
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx
So if I understood correctly, you want to read config files which are stored in a specific path?
Here is the trick:
Configuration _userConfig;
String configFilePath = "Your path here";
//Getting the user configuration object:
//Mapping the appropriate path:
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = configFilePath;
_userConfig = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
And there you go, _userConfig will contain the details in the path you specified :)
I have a WinForms .exe with an App.config that has a bunch of User Scoped Settings that are set at runtime and saved.
I want to be able to use the WinForms app to change and save the settings and then click on button to do some work based on the those settings.
I also want to read the user settings in the same .config file from a sep. console app so I can schedule to work to be done as a scheduled task.
What is the best way to be able to do this?
Update:
I tried the reccommendation of using ConfigurationManager.OpenExeConfiguration as described in some of the answers like so.
Configuration config = ConfigurationManager.OpenExeConfiguration("F:\\Dir\\App.exe");
but when I try to retrieve a User Setting like so.
string result = config.AppSettings.Settings["DB"].ToString();
I get a Null reference error.
From code in the exe however the following correctly returns the DB name.
Properties.Settings.Default.DB
Where am I going wrong?
Update 2:
So based on some of the answers below I now can use the following to retrieve the raw XML of the section of the user.config file I am interested in from sep. ConsoleApp.
System.Configuration.ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = #"D:\PathHere\user.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap,ConfigurationUserLevel.None);
System.Configuration.DefaultSection configSection = (System.Configuration.DefaultSection)config.GetSection("userSettings");
string result = configSection.SectionInformation.GetRawXml();
Console.WriteLine(result);
But I am still unable to just pull the value for the specific element I am interested in.
this should do it :
var clientSettingsSection = (System.Configuration.ClientSettingsSection)(ConfigurationManager.GetSection("userSettings/YourApplicationName.Properties.Settings"));
var setting = clientSettingsSection.Settings.Get("yourParamName_DB_");
string yourParamName_DB = ((setting.Value.ValueXml).LastChild).InnerText.ToString();
You can use the ConfigurationManager class to open the configuration file of another executable.
Configuration conf = ConfigurationManager.OpenExeConfiguration(exeFilePath);
// edit configuration settings
conf.Save();
See ConfigurationManager.OpenExeConfiguration
If you are setting these values per user basis you may need to use the ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel) method instead of the current method you are using now.
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = Environment.CurrentDirectory + "\\MyApp.exe.config";
//i have config in the same directory as my another app
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
var settingsSection = (System.Configuration.ClientSettingsSection)config.GetSection("userSettings/MyApp.Properties.Settings");
var setting = settingsSection.Settings.Get("MySetting");
var param = ((setting.Value.ValueXml).LastChild).InnerText.ToString();
If you know the path to the config file try:
System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration("configPath");
string myValue = config.AppSettings.Settings[ "myValue" ].Value;