.Net3.5中的app.config

发布于 2024-09-16 20:30:30 字数 1466 浏览 3 评论 0原文

我无法写入.Net3.5 中的App.conf。

根据this问题,它就像在.Net2.0中一样,但它不起作用。我可以像这样从配置中读取:

var connStr = ConfigurationManager.ConnectionStrings[ApplicationConstants.CisConnectionStringName].ConnectionString

但我不可能在该文件中写入,因为编译器表示由于安全级别的原因这是不可能的。

我用谷歌搜索了几个小时但没有找到解决方案。我希望你能帮忙。

谢谢

编辑:

我尝试了不同的方法:

ConfigurationManager.ConnectionStrings[ApplicationConstants.CisConnectionStringName].ConnectionString = "string";

也...

 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
NetSectionGroup netSectionGroup = config.GetSectionGroup("system.net") as NetSectionGroup;
netSectionGroup.Settings.HttpWebRequest.UseUnsafeHeaderParsing = true;                      
config.SaveAs(@"C:\ProgramData\test.config", ConfigurationSaveMode.Full);

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"C:\ProgramData\test.config");

也...

string exePath = System.Reflection.Assembly.GetEntryAssembly().CodeBase.Substring(0, System.Reflection.Assembly.GetEntryAssembly().CodeBase.LastIndexOf('/'));
        var configuration = ConfigurationManager.OpenExeConfiguration(exePath);
...

(示例对于给定路径无效,因为我测试了它们;-))

但没有任何效果。配置文件与 .exe 位于同一目录中(当前为“C:\Users\MyUser\SVN\CIS\Assemblies”)

I'm not able to write to the App.conf in .Net3.5.

According to this question it goes like in .Net2.0, but it doesn't work. I can read from the config like this:

var connStr = ConfigurationManager.ConnectionStrings[ApplicationConstants.CisConnectionStringName].ConnectionString

But it's impossible for me to write in that file, because the compiler says it's not possible because of the security level.

I googled for several hours but found no solution. I hope you can help.

Thx

Edit:

I tried different Ways:

ConfigurationManager.ConnectionStrings[ApplicationConstants.CisConnectionStringName].ConnectionString = "string";

also...

 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
NetSectionGroup netSectionGroup = config.GetSectionGroup("system.net") as NetSectionGroup;
netSectionGroup.Settings.HttpWebRequest.UseUnsafeHeaderParsing = true;                      
config.SaveAs(@"C:\ProgramData\test.config", ConfigurationSaveMode.Full);

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"C:\ProgramData\test.config");

also...

string exePath = System.Reflection.Assembly.GetEntryAssembly().CodeBase.Substring(0, System.Reflection.Assembly.GetEntryAssembly().CodeBase.LastIndexOf('/'));
        var configuration = ConfigurationManager.OpenExeConfiguration(exePath);
...

(Examples not valid for the given Path, as I tested it they where ;-))

But nothing worked. The config file is in the same directory as the .exe (currently "C:\Users\MyUser\SVN\CIS\Assemblies")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

z祗昰~ 2024-09-23 20:30:30

这对我有用:

 System.Configuration.Configuration config =ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        config.AppSettings.Settings["oldPlace"].Value = "3";     
        config.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("appSettings");

This worked for me:

 System.Configuration.Configuration config =ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        config.AppSettings.Settings["oldPlace"].Value = "3";     
        config.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("appSettings");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文