5

When I build a windows application. It does not work because it cannot read my app.config file. I looked in the bin directory and it does not have the appname.exe.config file. If I manually copy over the app.config and rename it the appname.exe.config the application will work..but when I build the project this file is never created automagically. What am I missing? Why is my project not creating it? I have looked through the whole folder structure and there is no other config files.

Greg P
  • 772
  • 2
  • 10
  • 23

5 Answers5

3

Everyone here is giving you false information I think. I think @bowlturner had it right. According to Microsoft, you do need to set the app.config's Copy to output directory property to either Copy Always or Copy if newer. From Microsoft's MSDN Site:

When you develop in Visual Studio, place the source configuration file for your app in the project directory and set its Copy To Output Directory property to Copy always or Copy if newer. The name of the configuration file is the name of the app with a .config extension. For example, an app called myApp.exe should have a source configuration file called myApp.exe.config.

Visual Studio automatically copies the source configuration file to the directory where the compiled assembly is placed to create the output configuration file, which is deployed with the app.

Community
  • 1
  • 1
Icemanind
  • 47,519
  • 50
  • 171
  • 296
  • 1
    **Visual Studio automatically copies the source configuration file to the directory where the compiled assembly is placed to create the output configuration file, which is deployed with the app.** This means you do not need to set "Copy to output directory" to `Copy Always` or `Copy if newer` – Hamid Pourjam Aug 06 '15 at 16:01
  • But that is the thing. with any of those settings it is not being created. – Greg P Aug 06 '15 at 16:02
  • @dotctor -- Yes, if you set the property to `Copy Always` or `Copy if newer`. Read the line above it: **When you develop in Visual Studio place the source configuration file for your app in the project directory and set its Copy To Output Directory property to Copy Always** – Icemanind Aug 06 '15 at 16:03
  • 1
    create a simple console or winforms application and see the properties on app.config. – Hamid Pourjam Aug 06 '15 at 16:05
  • 1
    @GregP - Delete your app.config file. Then right click on your project, and select add, add new item. Find "Application Configuration File" in the list and add it. – Icemanind Aug 06 '15 at 16:05
  • @dotctor - I created a Windows Application and added an `app.config`. By default, its set to `Do not copy` and it did indeed create the config file in the bin folder. But all I'm doing is quoting Microsoft's MSDN site. – Icemanind Aug 06 '15 at 16:09
  • @Icemanind - I have done that already and it didn't work. I just created a new project and it is still not doing it...frustration. – Greg P Aug 06 '15 at 16:53
  • I created a test windows form app and a console app..they already contained an app config, and it still is not creating..at a loss. – Greg P Aug 06 '15 at 16:57
2

The correct settings are:

  • Build Action = None
  • Copy to Output Directory = Do not copy

and very important, the project file needs to contain this element:

  <ItemGroup>
    <None Include="App.config" />
  </ItemGroup>
t3chb0t
  • 16,340
  • 13
  • 78
  • 118
1

The only thing that worked for me was to delete bin\ and obj\ entirely. Rebuild and the file is created fine.

Rich
  • 3,640
  • 3
  • 20
  • 24
  • Just to confirm with more details from my experience: I just had this happen when using Visual Studio 2019 (16.9.4) to build a .NET 4.7.2 console app. I copied an App.config into the project, and when I built the project it would not replace the default appname.exe.config in the bin folder. Tried various combinations of cleaning/rebuilding/deleting bin contents; finally deleting the bin/obj folders themselves fixed it. – hikarikuen May 06 '21 at 16:20
0

Look on App.config properties, should be:
BuildAction: None
CopyToOutputDirectory: Do not copy
Custom tool and custom tool namespace should be empty

Also try to Rebuild project. Right click on project -> Rebuild

Piotr Dory
  • 334
  • 2
  • 12
-2

Assuming you're using Visual Studio click your config file in the Solution Explorer and and in the Properties panel set Copy To Output Directory to something other than Do Not Copy.

JamJar00
  • 339
  • 4
  • 13