// Set LastRun to now
config.AppSettings.Settings["LastRun"].Value = DateTime.Now.ToString();
// Save all settings
config.Save(ConfigurationSaveMode.Modified);
This code was working fine in my development server but not in my production server. It seems like my program is unable to communicate with my app.config file. I have checked all the "obvious" . . Any ideas ... ?
From your code example, I cannot tell how your config variable is initialized. But, from the comments, you have a web app. Unless you are attempting to load a specific app.config file, the web app will attempt to get AppSettings from web.config.
It's not a good idea to programatically change the values of web.config. Changing web.config will cause an application restart.
If you have a different app.config for storing this type of information, that would be better than trying to change web.config. But you'll have to specifically load the file, something like this:
Configuration config = WebConfigurationManager.OpenWebConfiguration("yourPath\app.config");
ConfigurationManager.OpenExeConfiguration() is intended for use within an executable application not a web app. Try using WebConfigurationManager as shown above.
You find some more information in this SO question/answers.
More information can be found in this SO question/answer.
Related
I have a mvc .NET web application written in C# and I have a web.config file associated with it for web specific configuration values. I also have a windows service application that will be running on the server in the background that has a App.config associated with it. I have linked the file within the web application and can see the file with updated values. But I am unable to use those values in my controller to display them to the UI. Is there a way to make a call to the app.config values to use in the controller and views of the web application? Right now it seems like they are coming in null due to them not being in the web.config.
Any help is apprecaited.
As long as permissions are worked out, you should be able to open the shared config file thusly:
var map = new ExeConfigurationFileMap();
//TODO: resolve this path in whatever way makes sense for your situation.
map.ExeConfigFilename = #"C:\MyConfig.config";
var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
//do something with config, e.g. config.AppSettings.Settings["Blah"];
Otherwise, you can do something like put shared settings into machine.config, but it's typically wise not to mess with that file.
We have a WCF SerivceHost (self-hosted), XML-configured in App.config. The host has already been opened.
We want to change a specific configuration value in the service's binding at runtime (from code).
Unfortunately, our current approach:
((SomeBinding) _serviceHost.Description.Endpoint.Single().Binding).SomeProp = value;
does not work. The configuration is not "applied". How to force a configuration reload at runtime?
A possible approach is this:
Modify the value in the config file at runtime (cannot find instructions for doing this, but the approach here may give you a start)
Reload the config from file using:
ConfigurationManager.RefreshSection("system.serviceModel/bindings");
I don't think your current approach of having the config initially set in xml and then trying to update it via code once the service host has started will work.
I'm having azure worker role project. below is a code from my configuration (.cscfg).
c# (FLAG = 1;)
Through c# code i want to change this value.
Is there any way for dynamically changing value of an azure service configuration?
Please instruct me how to do it.
Simple answer is that you can't change the value of just one setting through code as RoleEnvironment class only exposes a Get method and there's no Set method.
A convoluted approach would be to invoke Service Management API, read the configuration settings file using Get Deployment operation, change the XML file and the call Change Deployment Configuration operation. Not sure how this would work out if you're changing the configuration from the same role though.
I am trying to access system.webServer/defaultDocument in my web.config file but can't seem to find out how. I have tried various articles about editing the web config file but I don't seem to have the same options to access the items in system.webServer as I would if I was changing a connectionString for instance.
I can load the system.webServer section using:
ConfigurationSection WebServerSection = (ConfigurationSection)WebConfigurationManager.GetSection("system.webServer");
but can't see to find anything usable from that point. One thing I have noticed is that the system.webServer section is of type System.Configuration.IgnoreSection. Does this somehow stop me having access to edit it?
Take a look the Microsoft.Web.Administration API, it offers a WebConfigurationManager class that should allow you to access the content of the webServer section:
WebConfigurationManager.GetSection(HttpContext.Current,
"system.webServer/defaultDocument");
I want to know that is there any property or method by which i come to know that a folder is there on the server.
just like if i have this in my web.config:
<appSettings>
<add key="ImagePath" value="http://server1:801/ImageById/"/>
</appSettings>
and i am getting this key like this:
var URL = System.Configuration.ConfigurationManager.AppSettings["ImagePath"].ToString();
Now i want to know that how to access imageById on the server and save something into this.
just like below:
if("Folder exist on this server URL that is ImageById")
{
save images to the folder thorugh code of WCF as the folder has write permission.
}
and i want this functionality in WCF not in ASP.NET.
please help.
if(Directory.Exists(Server.MapPath("ImabgeById")))
{
save images to the folder thorugh code of WCF as the folder has write permission.
}
also this can be of help
How to get working path of a wcf application?
You can use System.IO.Directory.Exists(path) - you simply need to ensure that your WCF service is operating under credentials with adequate permissions to get to the folder in question (otherwise you will encounter an exception).