简体   繁体   English

在c#控制台应用程序中,app.config的替代方案是什么?

[英]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.

相关问题 在 C# 控制台应用程序 app.config 中嵌入 webclient 的凭据? - Embed credentials for webclient in C# Console Application app.config? 控制台应用程序中的 App.Config 文件 C# - App.Config file in console application C# 如果我在C#应用程序中删除App.config会发生什么? - What happen if I delete App.config in C# application? 如何在 C# 控制台应用程序中动态加载外部 App.Config? - How to load external App.Config dynamically in C# Console application? 如何将 app.config 文件添加到项目(控制台应用程序 C#) - How to add an app.config file to a project (Console Application C#) 与console.exe应用程序C#一起编译App.config XML文件 - Compile App.config XML file along with console.exe application C# 如何让我的 c# 控制台应用程序与 app.config 一起运行? - How do I get my c# console application to run with an app.config? 循环通过 App.Config 文件并将每个值存储在 C# 控制台应用程序的变量中? - Loop Through App.Config file and store each value in variable for C# Console Application? C#VS2013发布后如何在控制台应用程序中使用App.config - C# VS2013 How to use App.config in a console application after publish it C#控制台库app.config连接字符串问题 - C# Console Library app.config connection string problem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM