Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is app.config required in .Net 4.0 C# projects?

Much to my annoyance, Visual Studio 2010 added App.config files to all of my .EXE projects when I changed the target from .NET Framework 3.5 to .NET Framework 4.0. So my automated build broke until I checked those new files in.

Is there any particular reason I can't just delete these App.config files? Or is Visual Studio going to keep adding them to my projects?

I'll refrain from ranting about a tool that adds useless files to my projects . . .

Clarification:

I understand what app.config is for. What I don't understand is why Visual Studio 2010 would add an app.config file to an existing project when I change the project to target .NET 4.0 rather than .NET 3.5. My application doesn't require anything from app.config in order to run, so my only reasonable conclusion would be that either Visual Studio for some reason needs the app.config in order to compile, or somebody decided that they know better than me and my application must have an app.config even though I don't want one.

Oops ... I ranted.

Further clarification:

Converting a project from Visual Studio 2008 to Visual Studio 2010 is not the problem. It's when I change the target from .NET Framework 3.5 to .NET Framework 4.0 that the app.config gets added to the project. The app.config contains this:

<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

When I change it back to target .NET Framework 3.5, the app.config file is modified to read:

<supportedRuntime version="v2.0.50727"/>

I think I see what they're trying to do, but adding that app.config file automatically was a really bad idea.

like image 906
Jim Mischel Avatar asked Sep 30 '10 17:09

Jim Mischel


People also ask

When should I use app config?

App. Config is an XML file that is used as a configuration file for your application. In other words, you store inside it any setting that you may want to change without having to change code (and recompiling). It is often used to store connection strings.

Why do we add config in C#?

By adding an application configuration file (app. config file) to a C# project, you can customize how the common language runtime locates and loads assembly files. For more information about application configuration files, see How the runtime locates assemblies (. NET Framework).

Where is .NET app config?

The machine configuration file, Machine. config, contains settings that apply to an entire computer. This file is located in the %runtime install path%\Config directory.


3 Answers

The only time I know of that app.config is required in a .NET 4 project is if you reference a CLR-2.0, mixed mode assembly. In that case, you need to add useLegacyV2RuntimeActivationPolicy="true" into the startup element, or you'll get errors at runtime.

However, the VS conversion tool does not detect this, and adds an app.config file by default that is really not necessary. If you are able to run after the conversion, this is likely not an issue for you.

Otherwise, you are free to delete it (unless, of course, you're using it for other purposes).

like image 98
Reed Copsey Avatar answered Oct 15 '22 11:10

Reed Copsey


The app.config file is not necessary. Applications will run just fine without them.

EDIT

Reed pointed out there is one case where this is not true that's worth noting

There is one situation where it is required - if your project references a CLR 2.0 mixed-mode assembly, you must have an app.config file to run

like image 38
JaredPar Avatar answered Oct 15 '22 12:10

JaredPar


There was probably some setting that needed to be added in the config file in the conversion, if you create a new project you don't get an app.config automatically.

You would have to check what it contains to determine if there is something in it that is needed.

like image 2
Guffa Avatar answered Oct 15 '22 11:10

Guffa