我将 WPF 应用程序的目标 .NET 框架从 3.5 更改为 4.0。
进行此更改后,我注意到 VS2010 生成了一个 app.config 文件并放置在主项目文件夹中。其构建操作设置为“无——不复制”。
此 app.config 文件包含以下 XML:
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
我的项目的 .csproj 文件包含:
<Project
ToolsVersion="4.0" DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
问题:
- 之间的含义有什么区别在 app.config 和 .csproj 中的
中?其中一个是否会凌驾于另一个之上,或者它们实际上意味着不同的事情?
- 如果不复制到输出目录,app.config 文件会执行任何操作吗?
- 我可以删除 app.config 文件而不破坏任何内容吗?
编辑
还有一个问题:
如果我从头开始创建一个 WPF 4.0 应用程序,为什么默认情况下没有创建 app.config 文件? (这是否意味着该文件是不必要的?)
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:
- 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?
- Does the app.config file do anything if not copied to the output directory?
- 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?)
发布评论
评论(2)
App.config 文件告诉 .Net 运行时该程序集需要 .Net 4.0。
它以
.exe.config
形式复制到 outout 文件夹。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
.这篇 Microsoft 博客文章对此进行了很好的解释:
http://blogs.msdn.com/b/jgoldb/archive/2010/04/12/what-s-new-in-net-framework-4 -client-profile-rtm.aspx
具体答案:
正如 SLaks 所说,.csproj 设置告诉编译器要使用哪个框架版本,而运行时 (CLR) 使用 app.config 文件来确定哪个(如果有) ) 用户需要下载的框架版本。
即使构建操作是“none -- do not copy”,app.config 也会被复制到输出中。 app.config 是一个特殊文件,会自动重命名为“[name_of_app].exe.config”,然后复制到输出目录。
对于 WPF 4.0 应用程序,如果应用程序面向 .NET 4.0 客户端配置文件,则可以省略 app.config。如果它面向完整的 .NET 4.0 框架(包括 asp.net 等),则必须保留该文件。
默认情况下不创建此文件的原因是 WPF 4.0 应用程序默认以 .NET Framework 4.0 Client Profile 为目标。由于运行时将假定所有 .NET 4.0 应用程序只需要安装客户端配置文件,因此一切都会好起来的。
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
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.