.Net5+的应用配置文件

在Visual Studio项目中,”.exe.config” 文件(也称为应用程序配置文件)和清单文件(manifest file)是两个不同的文件类型,分别用于不同的目的。以下是它们的主要区别和用途:

应用程序配置文件(”.exe.config”)

1. 目的
应用程序配置文件用于存储应用程序的可配置设置,如数据库连接字符串、应用程序设置、日志配置等。这些设置可以在运行时读取和更改,而无需重新编译应用程序。

2. 位置
配置文件通常命名为 ”App.config”,在编译时会被复制并重命名为 ”YourApplicationName.exe.config”,存放在输出目录中(如 ”bin\Debug” 或 ”bin\Release”)。

3. 格式
配置文件是XML格式的,包含不同的配置节,如 ”appSettings”、”connectionStrings”、自定义配置节等。

4. 示例

xml
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Setting1" value="Value1" />
<add key="Setting2" value="Value2" />
</appSettings>

<connectionStrings>
<add name="MyDatabase" connectionString="Server=myServer;Database=myDB;User Id=myUser;Password=myPass;" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>

  

5. 读取方式
在C#代码中,使用 ”ConfigurationManager” 类读取配置文件中的设置:

csharp
using System;
using System.Configuration;

class Program
{
static void Main()
{
string setting1 = ConfigurationManager.AppSettings["Setting1"];
Console.WriteLine("Setting1: " + setting1);
}
}

  

清单文件(Manifest File)

1. 目的
清单文件用于描述应用程序的元数据,如应用程序的依赖关系、请求的权限、Windows版本兼容性、UAC(用户帐户控制)设置等。它主要用于配置应用程序的运行时行为和环境。

2. 位置
清单文件通常与应用程序的可执行文件一起放置,命名为 ”YourApplicationName.exe.manifest”。在Visual Studio中,它通常被嵌入到应用程序的资源中。

3. 格式
清单文件也是XML格式的,包含应用程序的元数据和配置信息。

4. 示例

xml
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<description>My Application</description>
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*" />
</dependentAssembly>
</dependency>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/> <!-- Windows 10 -->
</application>
</compatibility>
</assembly>

  

5. 配置方式
在Visual Studio中,可以通过项目属性来配置清单文件:
1. 右键点击项目,选择“属性”。
2. 在“应用程序”选项卡中,点击“视图 Windows 设置”。
3. 在“视图 Windows 设置”对话框中,可以编辑清单文件。

总结

- 应用程序配置文件(”.exe.config”):用于存储应用程序的可配置设置,可以在运行时读取和更改。
- 清单文件(Manifest File):用于描述应用程序的元数据和运行时行为,配置应用程序的依赖关系、权限和兼容性。

这两个文件虽然都是XML格式,但它们的用途和内容完全不同,分别用于不同的配置需求。

posted @ 2024-08-08 14:10  卖雨伞的小男孩  阅读(2)  评论(0编辑  收藏  举报