CipherIS
asked on
C# - Read / Write Data to App.Config file in a specific directory
I have an app.config file which I'm storing in a different directory structure. Is it possible to Read / Write to that config file?
Read
--App.Config
MyApp
--MyApp.exe
--App.config
I want to access App.Config in "MyDir" Directory
Read
key = ConfigurationManager.AppSettings[ReportName];
Writeconfig.AppSettings.Settings.Add("key1", "value");
MyDir--App.Config
MyApp
--MyApp.exe
--App.config
I want to access App.Config in "MyDir" Directory
ASKER
That is what I thought. I tried that.
ConfigurationElement.this[ Configurat ion.Proper ty] is inaccessible due to its protection level.
For sConfig I'm passing in like "C:\MyDir\App.config".
Configuration config = ConfigurationManager.OpenExeConfiguration(sConfig);
key = config.AppSettings[ReportName];
However for "config.AppSettings" I receive an error ConfigurationElement.this[
For sConfig I'm passing in like "C:\MyDir\App.config".
ASKER
Below is my Code. Still receiving an error.
string key = string.Empty;
string sConfig = string.Empty;
Files files = new Files();
sConfig = Path.Combine( files.ReturnSaveLocation(), @"App.Config");
Configuration config = ConfigurationManager.OpenExeConfiguration(sConfig);
ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
configFile.ExeConfigFilename = sConfig;
Configuration mConfig = ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None, true);
string s = aem.ReportName;
key = mConfig.AppSettings[s]; //Receiving error here. Inaccessible due to protection level.
ASKER
Having issues with the app.config. Looking at just reading/writing to XML.
Is there a way to read write to XML so I can have similar result?
<key=1> <value=123>
<key=2> <value=abc>
I want to be able to check to see if key 1 exists and if it doesn't add it to the XML file along with it't value and so on.
Thanks.
Is there a way to read write to XML so I can have similar result?
<key=1> <value=123>
<key=2> <value=abc>
I want to be able to check to see if key 1 exists and if it doesn't add it to the XML file along with it't value and so on.
Thanks.
ASKER
Hope this makes sense. Check the xml for Key value 3.
<?xml version="1.0" encoding="utf-8"?>
<Reports>
<report>
<key>1</>
<value>123</value>
</report>
<report>
<key>2</>
<value>abc</value>
<report>
</Reports>
if it doesn't exist then add it.<?xml version="1.0" encoding="utf-8"?>
<Reports>
<report>
<key>1</>
<value>123</value>
</report>
<report>
<key>2</>
<value>abc</value>
</report>
<report>
<key>3</>
<value>12ab</value>
</report>
</Reports>
If it does exist then update it<?xml version="1.0" encoding="utf-8"?>
<Reports>
<report>
<key>1</>
<value>123</value>
</report>
<report>
<key>2</>
<value>abc</value>
</report>
<report>
<key>3</>
<value>ab12</value>
</report>
</Reports>
ASKER
This is what I was able to come up with so far.
It is inserting key and value but not correctly. It is inserting it as
<key>1<value>123</value</k ey>
Can't get <report> added. Don't know if I need to or not.
Any suggestions?
public class XML
{
public string ReadXML(string key)
{
bool found = false
return found;
}
public bool WriteXML(string sKey, string sValue)
{
Files file = new Files();
string saveLocation = file.ReturnSaveLocation();
string xml = string.Empty;
xml = Path.Combine(saveLocation, "Reports.xml");
XmlDocument doc = new XmlDataDocument();
doc.Load(xml);
XmlNamespaceManager namespaces = new XmlNamespaceManager(doc.NameTable);
namespaces.AddNamespace("Reports", "http://www.w3.org/2001/XMLSchema");
XmlNode nextNode = doc.SelectSingleNode("/Reports", namespaces);
XmlElement report = doc.CreateElement("Export");
XmlElement key = doc.CreateElement("Key");
key.InnerText = sKey;
XmlElement value = doc.CreateElement("Value");
value.InnerText = sValue;
nextNode.AppendChild(key);
key.AppendChild(value);
doc.Save(xml);
return true;
}
}
It is inserting key and value but not correctly. It is inserting it as
<key>1<value>123</value</k
Can't get <report> added. Don't know if I need to or not.
Any suggestions?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks so much. The code is cleaner and easier to understand also.
Not a problem CipherIS, glad I was able to help.
You should be able to get an instance of the configuration object and use that to access the section of the config file. Please see this Microsoft doc which has an example. ConfigurationManager.OpenM