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

发布于 2024-09-16 19:47:46 字数 147 浏览 1 评论 0原文

我想将连接字符串和一些参数存储在 app.config 文件中,我们通常为 Windows 应用程序执行此操作,但我找不到控制台应用程序的 app.config 文件。那么我应该如何使用这个文件,如何添加这个文件,或者还有一些其他工作可以实现相同的功能。我正在控制台应用程序中工作

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. 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

温折酒 2024-09-23 19:47:46

右键单击应用程序 -> 转到添加 -> 您将看到我在此处附加的确切图片 -> 选择应用程序配置文件。替代文本

Right click on application->Go to Add->you will see the exact picture What i have attached here->Pick the Application Config File.alt text

找个人就嫁了吧 2024-09-23 19:47:46

好吧,其他答案可能对其他人有用,但对我来说,将设置文件添加到项目的属性中解决了问题 - 它实际上将设置(可通过可视设计器编辑)序列化到我的配置文件中。因此,这样,这里其他答案中显示的配置文件方法对我来说不起作用,但创建一个设置文件确实有效。

步骤是:

项目(不是解决方案)>添加>新商品>设置文件

添加设置文件

此外,您可能希望在代码中使用强类型值来提供设置。我执行了以下操作:

  1. 将设置文件重命名为有用的内容 - 我的是“Settings.settings”
  2. 将此文件移动到“项目>属性”部分
  3. 双击
  4. 设计器中的设置文件图标,添加了设置键和值
  5. Viola!您可以在应用程序中使用强类型值的设置!

所以,现在我可以像这样访问我的设置(从我的控制台应用程序):

bool boolSetting = Properties.Settings.Default.is_debug_mode;

编译后,我发现这些设置自动存储在名为“AssemblyName.exe.config”的文件中,与控制台二进制文件本身一起存储在 调试目录。

所以,我认为这是创建和管理应用程序配置文件的一种更干净、更灵活的方式。

注意:我正在运行 Visual Studio Ultimate 2012,并正在构建 .NET 3.5 控制台应用程序。

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

Adding 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"
  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.

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.

权谋诡计 2024-09-23 19:47:46

使用上述 VS2017 的任何人,如果您没有看到图像中突出显示的选项应用程序配置文件
那么很可能你还没有安装.net桌面开发工具。

应用程序配置文件模板来自.net桌面开发工作负载,因此您需要确保已将其安装在VS2019/VS2022中。(Go tools=>get tools and features=>make确保您已选中 .net 桌面开发工作负载,或者使用 Visual Studio 安装程序安装所需的包。

Anyone using VS2017 above and if you are not seeing the option highlighted in image Application Config File
Then most probably you have not installed .net desktop development tools.

The application configuration file template comes from .net desktop development workload, so you need to make sure you've installed it in VS2019/VS2022.(Go tools=>get tools and features=>make sure you have .net desktop development workload checked. Or Use Visual Studio Installer to install the required package.

末骤雨初歇 2024-09-23 19:47:46

遇到同样的问题,只需打开“项目”选项卡并按“添加表单”(Windows 窗体),然后搜索 config,您将找到 app.config

Had the same problem, just open the Project tab and press Add Form (Windows Forms) then search for config and you will find app.config

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文