I am Trying to get the configuration file using the following code:
public void EncryptConnString()
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Server.MapPath(#"/"));
ConfigurationSection section = config.GetSection("connectionStrings");
if (!section.SectionInformation.IsProtected)
{
config.Save(ConfigurationSaveMode.Modified);
}
}
But i am getting the error
The relative virtual path 'F:/xxxx/yyyy/sample/' is not allowed here.
Note: I am accessing this code in global.asax page What i am doing wrong?
If you pass null to this method, it will return the root config file for you:
var config = WebConfigurationManager.OpenWebConfiguration(null);
Related
I am trying to read the custom-defined configuration file from the library project that I have created in c#.
To read the configuration file, There is a ConfigManager i have created as below,
static Configuration ConfigManager
{
get
{
if (_configuration == null)
{
//Map the new configuration file.
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = _assemblyLocation;
// Get the mapped configuration file
_configuration = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
}
return _configuration;
}
}
I am getting the exception at OpenMappedExeConfiguration() Method.
Any help would be appreciated.
Thanks in advance.
Attempts to use Configuration.Save() fail when the WPF program is installed under "Program Files". If installed elsewhere, the Save() works as expected. Isn't the program itself authorized to edit its own .config file?
I get two exceptions:
System.Configuration: An error occurred loading a configuration file: Access to the path 'C:\Program Files\Advanced Applications\ConfigurationTest\ejoqasrr.tmp' is denied. (C:\Program Files\Advanced Applications\ConfigurationTest\WPF.exe.Config)
mscorlib: Access to the path 'C:\Program Files\Advanced Applications\ConfigurationTest\ejoqasrr.tmp' is denied.
I tried opening with ConfigurationUserLevel.PerUserRoaming) instead of ConfigurationUserLevel.None but it still failed, reporting different exceptions:
System.Configuration: An error occurred executing the configuration section handler for appSettings.
System.Configuration: ConfigurationSection properties cannot be edited when locked.
[NOTE: Searching found an article that appears related but didn't provide a resolution for this issue:
Access to path denied for App.Config on Windows 8. How can I update App.Config?
The first two methods are called from MainVM and invoke the underlying base methods which expect to find a correlated property on the passed in object and then synchronize the appropriate values in AppSettings.
private void SettingsLoad()
{
SettingsLoad(this);
}
private void SettingsSave()
{
try
{
SettingsSave(this);
}
catch (Exception ex)
{
currentMessageAction = MessageAction.Acknowledge;
window.MessagePanel.Show("EXCEPTION!", Utility.ParseException(ex));
}
}
....
public static void SettingsLoad(object main)
{
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
foreach (KeyValueConfigurationElement setting in config.AppSettings.Settings)
{
// Attempt to get a reference to the property and, if found, set its value
var prop = main.GetType().GetProperty(setting.Key);
if (prop != null)
{
prop.SetValue(main, setting.Value);
}
}
}
public static void SettingsSave(object main)
{
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
foreach (KeyValueConfigurationElement setting in config.AppSettings.Settings)
{
// Attempt to get a reference to the property and, if found, update its value
var prop = main.GetType().GetProperty(setting.Key);
if (prop != null)
{
setting.Value = prop.GetValue(main).ToString();
}
}
// This line produces an exception
config.Save(ConfigurationSaveMode.Modified);
}
Isn't the program itself authorized to edit its own .config file?
No. The user account under which the application is running must have permissons to write to the folder/file.
Apparently it doesn't have the appropriate permissions to write to the "Program Files" folder in your case.
How to Set File and Folder Permissions in Windows
I'm trying to save a string to the AppSettingsSection of the current application's default configuration in the roaming profile using ConfigurationManager (ConfigurationUserLevel.PerUserRoaming).
When I'm saving to the local profile (ConfigurationUserLevel.None) it works just fine.
// Write Name in NameSaved Section of AppSettings
public void WriteNameToAppSettings(string nameToSave)
{
// Open Config File
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming); // ConfigurationUserLevel.None (local) is working just fine...
// Add or Update NameSaved Section of AppSettings
if ((configuration.AppSettings.Settings["NameSaved"]?.Value) == null)
configuration.AppSettings.Settings.Add("NameSaved", nameToSave);
else
configuration.AppSettings.Settings["NameSaved"].Value = nameToSave;
// Save and Refresh Config File
configuration.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(configuration.AppSettings.SectionInformation.Name);
}
// Set LastEnteredName Property from NameSaved Section of AppSettings
public void ReadNameFromAppSettings()
{
// Read Config File and then Get Name LastEntered Section of AppSettings
LastEnteredName = ConfigurationManager.AppSettings["NameSaved"]?.ToString() ?? ""; // If null default to ""
}
The error I'm getting is:
System.InvalidOperationException: 'ConfigurationSection properties cannot be edited when locked.'
Any clues on how to fix this?
I am getting an InvalidArgumentException when I run my application. I am attempting to create a new ExeConfigurationFileMap, and then load it with ConfigurationManager.
public static ExeConfigurationFileMap configFile = new ExeConfigurationFileMap(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\QuikSnap\\QuikSnap.config");
public static Configuration config = ConfigurationManager.OpenMappedExeConfiguration(Settings.configFile, ConfigurationUserLevel.None);
I have also attempted to set the configuration file after declaring it, but still didn't have any luck.
If I attempt to continue after this exception, I next receive a TypeInitalizationException upon trying to set a variable to one of the values in the configuration file.
Ran into this same problem. For some ridiculous reason, initializing ExeConfigurationFileMap even with the filepath does not set the property ExeConfigFilename which is required by the Configuration objects constructor. I fixed it by immediately setting that property after instantiating the ExeConfigurationFileMap object like below:
public static ExeConfigurationFileMap configFile = new ExeConfigurationFileMap(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\QuikSnap\\QuikSnap.config");
configFile.ExeConfigFilename = "QuikSnap.config";
public static Configuration config = ConfigurationManager.OpenMappedExeConfiguration(Settings.configFile, ConfigurationUserLevel.None);
You didn't set the right property with the value of the config file path.
Also, you have a static variable dependency on another static variable in the same class. There could be potentially an issue of order of execution here (though i'm not sure)
Try this instead:
public static Configuration config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap()
{
ExeConfigFilename = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\QuikSnap\\QuikSnap.config"
}, ConfigurationUserLevel.None);
I have an installer class which is retrieving the parameters from the Visual Studio Setup project.Now on opening the exeConfiguration i am getting following error..
Error 1001:An error occurred loading a configuration file.
The parameter 'exePath' is invalid.
Parameter name:exepath-->The Parameter 'exePath' is invalid.
Parameter name:exePath
And here is my Installer.cs Code..
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
string targetDirectory = Context.Parameters["targetdir"];
string param1 = Context.Parameters["Param1"];
string param2 = Context.Parameters["Param2"];
string param3 = Context.Parameters["Param3"];
string exePath = string.Format("{0}TechSoft CallBill.exe", targetDirectory);
Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
config.AppSettings.Settings["Param1"].Value = param1;
config.AppSettings.Settings["Param2"].Value = param2;
config.AppSettings.Settings["Param3"].Value = param3;
config.Save();
}
Please help me to sort out this error As i am not able to figure out.
Any suggestion is heartily welcomed.
Thanks in advance
If the Config file belongs to the same project then try this, If the config file belongs to the same project then you don't have to use config file path.
Configuration config= ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["Param1"].Value = param1;
config.AppSettings.Settings["Param2"].Value = param2;
config.AppSettings.Settings["Param3"].Value = param3;
//Save only the modified section of the config
config.Save(ConfigurationSaveMode.Modified);
//Refresh the appSettings section to reflect updated configurations
ConfigurationManager.RefreshSection(Constants.AppSettingsNode);