简体   繁体   English

.Net3.5中的app.config

[英]app.config in .Net3.5

I'm not able to write to the App.conf in .Net3.5. 我无法在.Net3.5中写入App.conf。

According to this question it goes like in .Net2.0, but it doesn't work. 根据这个问题,它就像在.Net2.0中一样,但是不起作用。 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") 配置文件与.exe在同一目录中(当前为“ C:\\ Users \\ MyUser \\ SVN \\ CIS \\ Assemblies”)

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM