当我将目标框架更改为 4.0 时,为什么我的 WPF 3.5 应用程序获得了 app.config 文件?

发布于 2024-10-05 19:25:57 字数 1029 浏览 0 评论 0 原文

我将 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">

问题:

  1. 之间的含义有什么区别在 app.config 和 .csproj 中的 中?其中一个是否会凌驾于另一个之上,或者它们实际上意味着不同的事情?
  2. 如果不复制到输出目录,app.config 文件会执行任何操作吗?
  3. 我可以删除 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:

  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?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

別甾虛僞 2024-10-12 19:25:57

App.config 文件告诉 .Net 运行时该程序集需要 .Net 4.0。
它以 .exe.config 形式复制到 outout 文件夹。

  1. .csproj 仅在编译时使用。
  2. 不; App.config 是由运行时从程序集的目录中读取的。
  3. 不,您不应该这样做。

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.
如梦 2024-10-12 19:25:57

这篇 Microsoft 博客文章对此进行了很好的解释:

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

app.config 文件是怎么回事?

如果您将项目更改为面向完整框架,VS 将添加一个配置文件 (app.config),将应用程序声明为“完整”应用程序。

替代文字

这使得 CLR 加载程序能够阻止任何在仅具有客户端配置文件的计算机上以完全为目标的 NET4 应用程序。在这种情况下,CLR 会提示用户安装 NET4 full。

例如,您可能会看到此对话框:

替代文字

请注意,在 NET4 Beta1 和 NET3.5 SP1 客户端配置文件中,如果 app.config 缺少 CLR,则假设您的目标是完整框架。现在情况已经逆转。
换句话说,如果您的 NET4 应用程序缺少 app.config,默认情况下 CLR 会假定您的应用程序面向 NET4 Client Profile!因此,您的应用程序在需要加载程序集时可能会随机崩溃客户资料中没有的内容。

具体答案:

  1. 正如 SLaks 所说,.csproj 设置告诉编译器要使用哪个框架版本,而运行时 (CLR) 使用 app.config 文件来确定哪个(如果有) ) 用户需要下载的框架版本。

  2. 即使构建操作是“none -- do not copy”,app.config 也会被复制到输出中。 app.config 是一个特殊文件,会自动重命名为“[name_of_app].exe.config”,然后复制到输出目录。

  3. 对于 WPF 4.0 应用程序,如果应用程序面向 .NET 4.0 客户端配置文件,则可以省略 app.config。如果它面向完整的 .NET 4.0 框架(包括 asp.net 等),则必须保留该文件。

  4. 默认情况下不创建此文件的原因是 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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文