简体   繁体   中英

Use Configuration File with Stand-Alone C# exe

I have developed a C# application based on the .NET framework in Visual Studio and have created a supplemental "App.config" file which I use to define certain variables in my Main.

Within Visual Studio, the config file seems to work fine and uses ConfigurationManager.AppSettings.Get("name") to define variables.

When I compile the application to a separate .exe and place it in the same directory as the "App.config" file, however, it fails to define the variables using the config file.

Is there any way to have a stand-alone executable that pulls information from the config file? How should it be set up?

Try this:

Before compiling your solution, in File Explorer of Visual Studio, right click your App.config file and select Properties .

On Build Action property of the file, set it as Embedded Resource .

On Copy to Output Directory property of the file, set it as Copy always .

Build your solution on Release and give it a try, hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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