如何为.NET控制台应用程序设置配置文件?

时间:2021-01-30 03:52:03

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

7 个解决方案

#1


30  

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.

要在项目中添加一个项目,请右键单击解决方案资源管理器中的项目,添加...,新项目...并从模板框中选择“应用程序配置文件”。

#2


14  

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。

#3


3  

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网格中的任何值(行)时,在“属性”窗格中注意GenerateDefaultValueInCode属性(在我的情况下为VS2008)。如果将GenerateDefaultValueInCode设置为True(True是默认值!),则默认值将编译到exe(或dll)中,当您在纯文本编辑器中打开它时,可以在文件中找到它。我正在使用控制台应用程序,如果我在exe中有默认值,应用程序总是忽略放在同一目录中的配置文件!相当噩梦,并没有在整个互联网上的这方面的信息。

#4


0  

Yes, it's possible. You just need to make an app.config file.

是的,这是可能的。你只需要制作一个app.config文件。

#5


0  

Yes. Look up "application configuration file" in the documentation.

是。在文档中查找“应用程序配置文件”。

#6


0  

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 @Dylan says 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的内容交给另一个dev和他可以将它们直接放在他的web.config中。非常便利。

#7


0  

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

在Windows Forms C#应用程序中拥有配置文件的最简单方法

#1


30  

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.

要在项目中添加一个项目,请右键单击解决方案资源管理器中的项目,添加...,新项目...并从模板框中选择“应用程序配置文件”。

#2


14  

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。

#3


3  

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网格中的任何值(行)时,在“属性”窗格中注意GenerateDefaultValueInCode属性(在我的情况下为VS2008)。如果将GenerateDefaultValueInCode设置为True(True是默认值!),则默认值将编译到exe(或dll)中,当您在纯文本编辑器中打开它时,可以在文件中找到它。我正在使用控制台应用程序,如果我在exe中有默认值,应用程序总是忽略放在同一目录中的配置文件!相当噩梦,并没有在整个互联网上的这方面的信息。

#4


0  

Yes, it's possible. You just need to make an app.config file.

是的,这是可能的。你只需要制作一个app.config文件。

#5


0  

Yes. Look up "application configuration file" in the documentation.

是。在文档中查找“应用程序配置文件”。

#6


0  

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 @Dylan says 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的内容交给另一个dev和他可以将它们直接放在他的web.config中。非常便利。

#7


0  

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

在Windows Forms C#应用程序中拥有配置文件的最简单方法