如何为 .NET 控制台应用程序设置配置文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/167671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I set up a configuration file for .NET console applications?
提问by mmattax
Is it possible to use a ".net configuration" file for a .NET console application?
是否可以将“.net 配置”文件用于 .NET 控制台应用程序?
I'm looking for an equivalent to web.config, but specifically for console applications...
我正在寻找等效于 web.config,但专门用于控制台应用程序...
I can certainly roll my own, but If I can use .NET's built in configuration reader then I would like to do that...I really just need to store a connection string...
我当然可以自己动手,但是如果我可以使用 .NET 的内置配置阅读器,那么我想这样做……我真的只需要存储一个连接字符串……
Thanks
谢谢
回答by Dylan Beattie
Yes - use app.config.
是 - 使用 app.config。
Exactly the same syntax, options, etc. as web.config, but for console and WinForms applications.
与 web.config 完全相同的语法、选项等,但适用于控制台和 WinForms 应用程序。
To add one to your project, right-click the project in Solution Explorer, Add..., New Item... and pick "Application Configuration File" from the Templates box.
要将一个添加到您的项目,请在解决方案资源管理器中右键单击该项目,添加...,新建项目...,然后从模板框中选择“应用程序配置文件”。
回答by Greg Ogle
app.config... If you have an App.config in your project, it will get copied as executableName.exe.config in the case of a console application.
app.config... 如果您的项目中有 App.config,在控制台应用程序的情况下,它将被复制为 executableName.exe.config。
回答by Roman
This might help to some people dealing with Settings.settings and App.config: Watch out for GenerateDefaultValueInCode attribute in the Properties pane while editing any of the value (rows) in the Settings.settings grid in Visual Studio (VS2008 in my case). If you set GenerateDefaultValueInCode to True (True is the default here!), the default value is compiled into the exe (or dll), you can find it embeded in the file when you open it in a plain text editor. I was working on a console application and if I had defaults in the exe, the application always ignored the config file placed in the same directory! Quite a nightmare and no information about this on the whole internet.
这可能对某些处理 Settings.settings 和 App.config 的人有所帮助:在 Visual Studio 中编辑 Settings.settings 网格中的任何值(行)(在我的情况下为 VS2008)时,请注意“属性”窗格中的 GenerateDefaultValueInCode 属性。如果您将 GenerateDefaultValueInCode 设置为 True(此处为 True 是默认值!),则默认值会被编译到 exe(或 dll)中,您可以在纯文本编辑器中打开它时发现它嵌入在文件中。我正在开发一个控制台应用程序,如果我在 exe 中有默认值,该应用程序总是忽略放置在同一目录中的配置文件!真是一场噩梦,整个互联网上都没有关于这方面的信息。
回答by Darren Kopp
Yes, it's possible. You just need to make an app.config file.
是的,这是可能的。你只需要制作一个 app.config 文件。
回答by Robert Rossney
Yes. Look up "application configuration file" in the documentation.
是的。在文档中查找“应用程序配置文件”。
回答by cori
SInce I haven't fully made the leap to TDD yet (though I hope to on some upcoming project) I use a console app to test my library code that I produce for another web developer in our company to use.
由于我还没有完全过渡到 TDD(尽管我希望在一些即将到来的项目中实现)我使用控制台应用程序来测试我为我们公司的另一个 Web 开发人员编写的库代码。
I use app.config for all of those settings, and as @Dylansays above the syntax is exactly the same between that and web.config, which means I can also just hand the content of my app.config over to the other dev and he can put them directly in his web.config. Very handy.
我将 app.config 用于所有这些设置,正如上面@Dylan所说,它和 web.config 之间的语法完全相同,这意味着我也可以将 app.config 的内容交给另一个开发人员,然后他可以将它们直接放在他的 web.config 中。非常便利。
回答by Fabio Gomes
I asked almost the same question some days ago and got really good answers, take a look:
几天前我问了几乎同样的问题,得到了很好的答案,看看:
Simplest way to have a configuration file in a Windows Forms C# Application

