简体   繁体   English

如何在控制台应用程序中添加App.Config文件

[英]How to add App.Config file in Console Application

I want to store the connection string and some parameters in app.config file which we generaly do for windows aplication but I can't find app.config file for console application. 我想将连接字符串和一些参数存储在app.config文件中,这对于Windows应用程序通常会执行,但是找不到控制台应用程序的app.config文件。 So how should I use this file, how to add this file or there is some other work arroud for the same functionality. 因此,我应该如何使用该文件,如何添加此文件,或者还有其他一些工作需要相同的功能。 I am working in console application 我正在控制台应用程序中工作

Right click on application->Go to Add->you will see the exact picture What i have attached here->Pick the Application Config File. 右键单击application-> Go to Add->,您将看到确切的图片我在这里附加的内容->选择Application Config File。 替代文字

Well, the other answers might have worked for others, but for me, adding a settings file to the project's properties solved the problem - it actually serialized the settings (which are editable via a visual designer) to the config file for me. 好吧,其他答案可能对其他人也有用,但是对我来说,在项目的属性中添加设置文件解决了该问题-它实际上将设置(可以通过可视设计器进行编辑)序列化为我的配置文件。 So this way, the config file approach showed in the other answers here didn't work for me, but instead creating a settings file did work. 因此,以这种方式,此处其他答案中显示的配置文件方法对我不起作用,但创建设置文件确实起作用。

The steps are: 这些步骤是:

Project (not solution) > Add > New Item > Settings File 项目(不是解决方案)>添加>新建项目>设置文件

添加设置文件

In addition, you might want to have your settings available in your code with strongly-typed values. 另外,您可能希望代码中具有强类型值的设置可用。 I did the following: 我做了以下事情:

  1. renamed the settings file to something useful - mine was "Settings.settings" 将设置文件重命名为有用的东西-我的名字是“ Settings.settings”
  2. moved this file to the "Project > Properties" section 将此文件移到“项目>属性”部分
  3. double-clicked the settings file icon 双击设置文件图标
  4. In the designer, added settings keys and values 在设计器中,添加了设置键和值
  5. Viola! 中提琴! You have the settings available in your app, with strongly-typed values! 您可以在应用中使用具有强类型值的设置!

So, now I could access my settings like this (from my console app): 因此,现在我可以从控制台应用程序访问这样的设置:

bool boolSetting = Properties.Settings.Default.is_debug_mode;

After compilation, I found that these settings are stored automatically in a file named "AssemblyName.exe.config", alongside the console binary itself in the Debug directory. 编译后,我发现这些设置会自动存储在名为“ AssemblyName.exe.config”的文件中,以及控制台二进制文件本身在Debug目录中。

So, I think this is a cleaner, and more flexible way of creating and managing the app's config file. 因此,我认为这是创建和管理应用程序配置文件的更干净,更灵活的方法。

NOTE : Am running Visual Studio Ultimate 2012, and am building a .NET 3.5 console app. 注意 :正在运行Visual Studio Ultimate 2012,并正在构建.NET 3.5控制台应用程序。

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

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