windows窗体应用程序_如何将配置文件添加到Windows窗体应用程序

windows窗体应用程序_如何将配置文件添加到Windows窗体应用程序

windows窗体应用程序

When you are creating a new Windows Forms application, it isn’t immediately obvious how to get a configuration file for your application. Your application configuration file is supposed to be called executablename.exe.config, and should be in the same directory as your application in order for the .NET framework to automatically use it.

当您创建新的Windows Forms应用程序时,如何为应用程序获取配置文件并不是很明显。 您的应用程序配置文件应该称为可执行文件名.exe.config,并且应与应用程序位于同一目录中,以便.NET Framework自动使用它。

But when you create the configuration file in the \bin\debug directory, it gets overwritten immediately when you do a new build. That’s slightly annoying.

但是,当您在\ bin \ debug目录中创建配置文件时,在执行新的构建时它将立即被覆盖。 有点烦人。

The proper way to add this file is to add new new configuration file called App.config to your project, which Visual Studio will automatically copy and rename to the same name as your executable’s name when you do a build.

添加此文件的正确方法是将新的名为App.config的新配置文件添加到您的项目中,Visual Studio将在执行构建时自动将其复制并重命名为与可执行文件的名称相同的名称。

windows窗体应用程序_如何将配置文件添加到Windows窗体应用程序

There’s a really simple way to do this.. simply go to the File \ Add New Item menu, or hit Ctrl+Shift+A

有一种非常简单的方法来执行此操作。只需转到“文件” \“添加新项”菜单,或按Ctrl + Shift + A

You’ll notice that it’s already set to App.config for you. Just hit the Open button.

您会注意到它已经为您设置为App.config。 只需点击“打开”按钮。

If you look in the Solution Explorer, you will see that the config file is in your project:

如果您在解决方案资源管理器中查看,则会看到配置文件在您的项目中:

windows窗体应用程序_如何将配置文件添加到Windows窗体应用程序

 Now build your application, and take a look in the \bin\debug\ folder. You’ll see that the configuration file has automatically been generated and named correctly for your executable:

现在构建您的应用程序,并查看\ bin \ debug \文件夹。 您会看到配置文件已自动生成并为可执行文件正确命名:

windows窗体应用程序_如何将配置文件添加到Windows窗体应用程序

If you change the name of the outputted file, Visual Studio is smart enough to generate the new filename correctly.

如果更改输出文件的名称,则Visual Studio足够聪明,可以正确生成新文件名。

Enjoy Configuring!

享受配置!

翻译自: https://www.howtogeek.com/howto/programming/how-to-add-a-configuration-file-to-your-windows-forms-application/

windows窗体应用程序