Reputation: 50
I am trying to edit a config file of another application with my Web Service. Currently I have:
[WebMethod]
public string EditConfig(string path)
{
string cfgPath = Path.Combine(path, "Compressor.exe.config");
var configMap = new ExeConfigurationFileMap { ExeConfigFilename = cfgPath };
var cf = ConfigurationManager.OpenMappedExeConfiguration(configMap,
ConfigurationUserLevel.None);
string hello = cf.AppSettings.Settings["SaveTo"].Value;
return hello;
//cf.AppSettings.Settings["RandomAddedKey"].Value = "Hello World!";
//cf.Save();
}
I have also tried:
var config = ConfigurationManager.OpenExeConfiguration(path);
var hello = config.AppSettings.Settings["SaveTo"].Value;
return hello;
Sadly I haven't had success with any of these, and before resorting to the XML Editor I needed to make sure that this isn't possible or I am doing something wrong? I have searched through StackOverFlow and the best results returned me an "Object reference not set to an instance of an object." Exception.
I have tried feeding the exe to the path the config and only the directory in several different applications.
In this case I want the Web Service to tell the Compressor what to compress and where to save it, but in the code I am just experimenting to get it working.
I am using .NET Framework 3.5 (Latest for web services). I have tried editing apps in different kinds of framework versions and the end result was the same.
Thank you in advance.
Upvotes: 0
Views: 121
Reputation: 5369
If I get it right, what you want to do is manipulate a configuration file of another application programmatically. Your problems have nothing to do with doing your manipulation inside a web service application. While I think it is relatively easy to manipulate a configuration file through the very same application, it is not such an easy task to do it from another application. I would suggest two approaches:
Use the .NET API to do it. Here you can find an example of manipulating configuration files remotely.
You could manipulate the specific configuration file, as every other XML file using the respective .NET libraries. Here you can see how to do it.
Hope I helped!
Upvotes: 1