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:
<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?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?)
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
.
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.
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:
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:
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With