I have a console application and it has app.config. When I run this code:
class Program
{
static void Main()
{
ConnectionStringsSection connSection = ConfigurationManager.GetSection("connectionStrings") as
ConnectionStringsSection;
if (connSection != null)
{
if (!connSection.SectionInformation.IsProtected)
connSection.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
else
connSection.SectionInformation.UnprotectSection();
}
Console.Read();
}
}
I get error: "This operation does not apply at runtime". I also tried giving permissions to my app.config but no luck.
What can the issue?
You can try the following:
static void Main()
{
// Get the current configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
ConnectionStringsSection connSection = config.GetSection("connectionStrings") as
ConnectionStringsSection;
if (connSection != null)
{
if (!connSection.SectionInformation.IsProtected)
connSection.SectionInformation.ProtectSection(null);
else
connSection.SectionInformation.UnprotectSection();
}
connSection.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);
Console.ReadKey();
}
I think you are supposed to use the OpenExeConfiguration
method in this scenario:
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(pathToExecutable);
ConnectionStringsSection connSection =
config .GetSection("connectionStrings") as ConnectionStringsSection;
the parameter pathToExecutable
should be the full path to the exe of your application, for example: "C:\application\bin\myapp.exe"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With