gpt4 book ai didi

c# - 加密连接字符串 : "This operation does not apply at runtime"

转载 作者:太空狗 更新时间:2023-10-30 00:16:16 25 4
gpt4 key购买 nike

我有一个控制台应用程序,它有 app.config。当我运行这段代码时:

 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();
}
}

我收到错误消息:“此操作不适用于运行时”。我也尝试授予我的 app.config 权限,但没有成功。

有什么问题?

最佳答案

您可以尝试以下方法:

 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();
}

关于c# - 加密连接字符串 : "This operation does not apply at runtime",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7849336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com