Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why did my WPF 3.5 app gain an app.config file when I changed the target framework to 4.0?

I changed the target .NET framework of my WPF app from 3.5 to 4.0.

After making this change, I noticed that an app.config file was generated by VS2010 and placed in the main project folder. Its build action is set to "none -- do not copy".

This app.config file contains the following XML:

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

The .csproj file for my project contains:

<Project
    ToolsVersion="4.0" DefaultTargets="Build"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

Questions:

  1. What is the difference in meaning between <supportedRuntime version="4.0" in app.config and <Project ToolsVersion="4.0" in .csproj? Does one override the other or do they actually mean different things?
  2. Does the app.config file do anything if not copied to the output directory?
  3. Can I delete the app.config file without breaking anything?

Edit

One more question:

If I create a WPF 4.0 app from scratch, why is no app.config file created by default? (And doesn't this imply that the file is unnecessary?)

like image 579
devuxer Avatar asked Feb 26 '23 16:02

devuxer


2 Answers

The App.config file tells the .Net runtime that the assembly requires .Net 4.0.
It gets copied to the outout folder as <YourApp>.exe.config.

  1. The .csproj is only used at compile time.
  2. No; App.config is read by the runtime from the assembly's directory
  3. No, you shouldn't.
like image 101
SLaks Avatar answered Feb 28 '23 15:02

SLaks


This Microsoft blog article explains it pretty well:

http://blogs.msdn.com/b/jgoldb/archive/2010/04/12/what-s-new-in-net-framework-4-client-profile-rtm.aspx

What’s the deal with app.config file?

If you change the project to target the Full Framework, VS will add a configuration file (app.config) that declares the application as a "full" application.

alt text

This enables the CLR loader to block any NET4 apps that target full on machines that only have the Client Profile. In this case, the CLR prompts the user to install NET4 full.

E.g. you may see this dialog:

alt text

Note that in NET4 Beta1 and NET3.5 SP1 Client Profile, if the app.config was missing the CLR, the assumption was that you targeted the Full Framework. This is now reversed. In other words, if your NET4 app is missing app.config, by default the CLR assumes that your app is targeting NET4 Client Profile! So, your app may crash at random when it needs to load the assemblies that aren't in the Client Profile.

Specific Answers:

  1. As SLaks said, the .csproj settings tells the compiler what framework version to use, while the app.config file is used by the runtime (CLR) to determine which (if any) framework version the user needs to download.

  2. app.config will be copied to the output even if the build action is "none -- do not copy". app.config is a special file that will automatically be renamed to "[name_of_app].exe.config" then copied to the output directory.

  3. For WPF 4.0 apps, app.config can be omitted if the app targets the .NET 4.0 Client Profile. If it targets the full .NET 4.0 framework (which includes things like asp.net), the file must be kept.

  4. The reason this file is not created by default is that WPF 4.0 apps target .NET Framework 4.0 Client Profile by default. Since the runtime will assume that all .NET 4.0 apps need only the Client Profile installed, everything will be fine.

like image 34
devuxer Avatar answered Feb 28 '23 15:02

devuxer