[英]What is the alternate to app.config in c# console application?
I have been successfully using an exe (output of console app that i wrote) along with its application config file for a while. 我已经成功使用exe(我编写的控制台应用程序的输出)及其应用程序配置文件已有一段时间了。 When i zipped them and sent it to my manager, he asked if i could get rid of the app.config file so the only thing that he copies in future is the exe.
当我压缩它们并将其发送给我的经理时,他问我是否可以摆脱app.config文件,因此他将来唯一要复制的就是exe。
Here is a gist of what my console app does. 这是我的控制台应用程序执行的要点。 For example, my app.config has the following.
例如,我的app.config具有以下内容。
<userSettings>
<myapp.Properties.Settings>
<setting name="productline" serializeAs="String">
<value>Electronics</value>
</setting>
</userSettings>
When i run the app like 当我像运行应用程序时
c:\>myapp.exe -productline Mechanical -save
it will update the productline settings to Mechanical. 它将产品线设置更新为“机械”。 When i run my app again, the productline is retained as Mechanical until i change it again.
当我再次运行我的应用程序时,产品线将保留为“机械”,直到我再次对其进行更改。
Back to the problem, I thought i will get rid of the app.config file and have a static variable 'productline' and update it. 回到问题,我想我将摆脱app.config文件,并拥有一个静态变量“ productline”并进行更新。 However, when i tested, the static variable is overridden only till i run the exe one time.
但是,当我测试时,静态变量仅在我运行一次exe之前被覆盖。 Next time i run the exe, it goes back to default 'Electronics'.
下次我运行该exe时,它将恢复为默认的“电子”。 I assume the second time i run the exe is like running the web application with iisreset so the static variables are fresh again.
我假设我第二次运行exe就像使用iisreset运行Web应用程序,因此静态变量又是新鲜的。
What is the best approach here? 最好的方法是什么? Can an app.config be embedded into the exe, so i can still use it but the output will be just one exe file?
可以将app.config嵌入到exe中,以便我仍然可以使用它,但是输出将只是一个exe文件? Or what is the alternate way to save and retain the variables in this scenario ?
或在这种情况下保存和保留变量的替代方法是什么?
If you have a constraint that prevents you from saving to a file then you could put your setting in the registry. 如果您有一个限制,无法保存到文件,则可以将设置放入注册表中。 The RegistryProxy class gives you easy access to the CurrentUser registry settings.
RegistryProxy类使您可以轻松访问CurrentUser注册表设置。 This documentation provides methods for creating and manipulating those settings.
本文档提供了用于创建和操作这些设置的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.