程序集、应用程序配置及App.config和YourSoft.exe.config .
转自:http://www.cnblogs.com/luminji/archive/2010/10/21/1857339.html
本章概要:
1:什么是程序集
2:程序集标识属性
3:强名称的程序集
3.1:强名称工作原理
4:配置文件
5:使用 DEVPATH 查找程序集
6:指定要使用的运行库版本
7:App.config和YourSoft.exe.config
1:什么是程序集
程序集是 .NET Framework 应用程序的构造块;程序集构成了部署、版本控制、重复使用、激活范围控制和安全权限的基本单元。程序集是为协同工作而生成的类型和资源的集合,这些类型和资源构成了一个逻辑功能单元。程序集向公共语言运行库提供了解类型实现所需要的信息。
程序集属性是提供程序集相关信息的值。属性分为以下几组信息:
程序集标识属性。
信息性属性。
程序集清单属性。
强名称属性。
2:程序集标识属性
三种属性与强名称(如果适用)一起决定程序集的标识:名称、版本和区域性。这些属性构成程序集的完整名称,并且在代码中引用程序集时需要这些属性。您可以使用属性来设置程序集的版本和区域性。编译器或程序集链接器 (Al.exe) 根据包含程序集清单的文件在创建程序集时设置名称值。
有关程序集属性的更多信息,参看http://msdn.microsoft.com/zh-cn/library/4w8c1y2s(VS.80).aspx
3:强名称的程序集
强名称是由程序集的标识加上公钥和数字签名组成的,其中,程序集的标识包括简单文本名称、版本号和区域性信息(如果提供的话)。它使用对应的私钥从程序集文件中生成。(程序集文件包含程序集清单,其中包含组成程序集的所有文件的名称和哈希。)
具有强名称的程序集只能使用其他具有强名称的程序集的类型。否则将会危及到该具有强名称的程序集的安全。
3.1:强名称工作原理
签名机制
1. 用SN.exe 生成一个key文件, 这个key文件包括一个public key 和一个private key.
2. 用这个key文件签名assembly时, 编译器将用private key签名程序集, 并将public key嵌入manifest中
3. 编译器哈希manifest中所有的assembly内容, 并将此哈希值各自assembly的FileDef Talbe中.
4. 当如上3步处理后, 编译器将哈希PE文件的整个内容(除authenticode signature, 强名称数据, PE头), 然后将此哈希值用private key签名. 得到RSA数字签名.
5. 将此数字签名嵌入到PE文件的CLR头
防修改机制
1. 当签名后的assembly安装到GAC, 系统会哈希PE文件(同签名), 得到哈希值
2. 系统读取PE文件中CLR头中的RSA签名, 并用public key(包含在manifest中)解密
3. 比较第1步得到的哈希值和第2步得到解密值是否一致, 从而判断其是否被修改.
4:配置文件
配置文件是可以按需要更改的 XML 文件。开发人员可以使用配置文件来更改设置,而不必重编译应用程序。管理员可以使用配置文件来设置策略,以便影响应用程序在计算机上运行的方式。
配置文件更多内容查看http://msdn.microsoft.com/zh-cn/library/1xtk877y(VS.90).aspx。
5:使用 DEVPATH 查找程序集
开发人员可能想确保他们正在生成的共享程序集能与多个应用程序一起正常使用。在开发周期内开发人员不用频繁地将程序集放在全局程序集缓存中,他们可以创建 DEVPATH 环境变量,让该变量指向程序集的生成输出目录。
例如,假设您正在生成名为 MySharedAssembly 的共享程序集,且输出目录是 C:/MySharedAssembly/Debug。可以将 C:/MySharedAssembly/Debug 置于 DEVPATH 变量中。然后必须在计算机配置文件中指定 <developmentMode> 元素。该元素告诉公共语言运行库使用 DEVPATH 来查找程序集。
共享程序集必须能够由运行库发现。 若要指定用于解析程序集引用的私有目录,请在配置文件中使用 <codeBase> 元素 或 <probing> 元素,如 指定程序集的位置 中所述。 还可以将程序集放在应用程序目录的子目录中。有关更多信息,请参见运行库如何定位程序集。
下面的示例说明如何使运行库在由 DEVPATH 环境变量所指定的目录中搜索程序集。
<configuration>
<runtime>
<developmentMode developerInstallation="true"/>
</runtime>
</configuration>
6:指定要使用的运行库版本
<?xml version ="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v1.1.4322" />
</startup>
</configuration>
7:App.config和YourSoft.exe.config
为了更加快速的使用配置信息而不自己写代码实现读写,我们在创建应用程序的时候应该使用App.config。创建完毕后,我们发现App.config的属性是:
![]()
以上是创建App.config后的默认设置,不要修改这些属性,编译你的解决方案,会在输出目录中发现生成了一个YourSoft.exe.config(假设你的应用程序名为YourSoft.exe)。下面有几点需要说明:
1:YourSoft.exe.config其实对应的就是你解决方案中的App.config。注意,千万不要以为在输出目录中它也会以App.config存在。
2:如果“复制到输出目录”属性你设置的是“复制”或者“较新则复制”,则App.config会被复制到输出目录。千万不要以为在输出目录中的App.config对应用程序会有任何意义。运行时默认还是会去读YourSoft.exe.config。
3:输出目录中YourSoft.exe.config的值不会自动保持和你解决方案中的App.config内容一致。你需要手动去设置YourSoft.exe.config中的值。
练习:
1.You are creating a strong-named assembly named Asse mbly1 that will be used in multiple applications.
Assembly1 will be rebuilt frequently during the development cycle. You need to ensure that each time the
assembly is rebuilt it works correctly with each application that uses it. You need to configure the computer on
which you develop Assembly1 such that each application uses the latest bu ild of Assembly1. Which two actions
should you perform? (Each correct answer presents part of the solution. Choose two.)
A. Create a DEVPATH environment variable that points to the build output directory for the strong-named
assembly.
B. Add the following XML element to the machine configuration filE.
<developmentMode developerInstallation="true"/>
C. Add the following XML element to the machine configuration filE.
<dependentAssembly>
<assemblyIdentity name="Assembly1" publicKeyToken="32ab4ba45e0a69a1" language="en-US"
version="*.*.*.*" />
<publisherPolicy apply="no" />
</dependentAssembly>
D. Add the following XML element to the configuration file of each application that uses the strong-named
assembly:
<supportedRuntime version="*.*.*.*" />
E. Add the following XML element to the configuration file of each application that uses the strong-named
assembly:
<dependentAssembly>
<assemblyIdentity name="Assembly1" publicKeyToken="32ab4ba45e0a69a1" language="en-US"
version="*.*.*.*" />
<bindingRedirect newVersion="*.*.*.*"/>
</dependentAssembly>
Answer: A, B
2.Your company uses an application named Application1 that was compiled by using the .NET Framework
version 1.0. The application currently runs on a shared computer on which the .NET Framework versions 1.0 and
1.1 are installed. You need to move the application to a new computer on which the .NET Framework versions
1.1 and 2.0 are installed. The application is compatible with the .NET Framework 1.1, but it is incompatible with
the .NET Framework 2.0. You need to ensure that the application will use the .NET Framework version 1.1 on the
new computer. What should you do?
A. Add the following XML element to the application configuration file.
<configuration>
<startup>
<supportedRuntime version="1.1.4322" />
<startup>
</configuration>
B. Add the following XML element to the application configuration file.
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Application1"
publicKeyToken="32ab4ba45e0a69a1" culture="neutral" />
<bindingRedirect oldVersion="1.0.3075.0" newVersion="1.1.4322.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
C. Add the following XML element to the machine configuration file.
<configuration>
<startup>
<requiredRuntime version="1.1.4322" />
<startup>
</configuration>
D. Add the following XML element to the machine configuration file.
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Application1"
publicKeyToken="32ab4ba45e0a69a1" culture="neutral" />
<bindingRedirect oldVersion="1.0.3075.0" newVersion="1.1.4322.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Answer: A
程序集、应用程序配置及App.config和YourSoft.exe.config .的更多相关文章
- app.config *.exe.config 和*.vshost.exe.config基础学习
一.问题描述 在使用config文件来保存一些参数,便于下次启动程序时自动加载上次设置的参数的功能时, 碰到个问题,vs2010下调试运行程序始终无法实现config记录上次参数值,而直接运行exe程 ...
- C#之app.config、exe.config和vshost.exe.config作用区别
vshost.exe.config是程序运行时的配置文本 exe.config是程序运行后会复制到vshost.exe.config app.config是在vshost.exe.config和exe ...
- Debug目录、Release目录,bin目录、obj目录,vshost.exe.config文件、.exe.config文件分析【C#】
原文地址:http://blog.csdn.net/itwit/article/details/6803338 今天写程序的时候,需要更改.exe.config文件中的引用的webservice地址, ...
- 如何使用DotNet 2.0中的应用程序配置 Settings.settings
对于桌面应用程序,常常会需要记录一些用户配置信息,早期的做法一般是使用读写INI文件的办法. 对于.NET应用程序,并没有提供直接操作INI文件的类,需要调用Win32API,具体办法可以参考: ...
- 使用配置文件(.settings、.config)存储应用程序配置
原文:使用配置文件(.settings..config)存储应用程序配置 引言 我不知大家早先是如何保存应用程序配置,以备下次打开时使用的,反正我开始学.Net的时候就去研究序列化,以二进制或XML格 ...
- C# Setting.settings . 用法 2 使用配置文件(.settings、.config)存储应用程序配置
引言 我不知大家早先是如何保存应用程序配置,以备下次打开时使用的,反正我开始学.Net的时候就去研究序列化,以二进制或XML格式的序列化来保存应用程序配置.这样每次都要建立单独的配置类,并书写读写配置 ...
- PHP 后台程序配置config文件,及form表单上传文件
一,配置config文件 1获取config.php文件数组, 2获取form 表单提交的值 3保存更新config.php文件,代码如下: $color=$_POST['color']; $back ...
- .Net 自定义应用程序配置
.Net 自定义应用程序配置 引言 几乎所有的应用程序都离不开配置,有时候我们会将配置信息存在数据库中(例如大家可能常会见到名为Config这样的表):更多时候,我们会将配置写在Web.config或 ...
- 【.net 深呼吸】自定义应用程序配置节
实际上,应用程序配置文件 App.config,是由各个节(Configuration Section)组成的,通常,配置节是按功能划分的,比如我们很熟悉的 appSettings.connectio ...
随机推荐
- 前端技能汇总 Frontend Knowledge Structure
Frontend Knowledge Structure 项目起源 还记得@jayli 的这幅前端知识结构图么. 图片的形式具有诸多的不便.缺失源图的我们,无法为此图贡献些什么,随着时间的迁移,或许有 ...
- js中location.search、split()HTML5中localStorage
1. location.search在客户端获取Url参数的方法 location.search是从当前URL的?号开始的字符串 如:http://www.baidu.com/s?wd=baidu&a ...
- chrome 浏览器 开发者工具 性能检测 参数解释
Sending is time spent uploading the data/request to the server. It occurs between blocking and waiti ...
- 简单版问卷调查系统(Asp.Net+SqlServer2008)
1.系统主要涉及以下几个表 问卷项目表(Q_Naire) 问卷题目表(Q_Problem) 题目类型表(Q_ProblmeType) 题目选项表(Q_Options) 调查结果表(Q_Answer) ...
- HDU 1712 ACboy needs your help (分组背包模版题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 有n门课,和m天时间.每门课上不同的天数有不同的价值,但是上过这门课后不能再上了,求m天里的最大 ...
- HDU 1561The more, The Better(树形DP)
HDU 1561 The more, The Better 题目大意就不说了 直接DP[i][j]表示i为跟节点的子树上攻克j个城堡的所能获得的最多宝物的数量 DP[fa][j] = MAX{DP[ ...
- 监听UITextFiled输入文字长度的变化
需求:如果UITextFiled文字的长度大于零,改变一个按钮的颜色 1.一开始想到的当然是UITextFiled的代理方法,但是在UITextFiled的代理方法中并没有监听这个的这一选项!!! 2 ...
- hdoj 5288 OO’s Sequence
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5288 //*************头文件区************* #include<ios ...
- Linux 调节屏幕亮度
intel的核心显卡驱动是在 /sys/class/backlight/intel_backlight/ 目录下面的brightness文件中配置的. 可以通过查看max_brightness的值来确 ...
- MFC打开文件对话框
{ CString FilePathName; CFileDialog dlg(TRUE);///TRUE为OPEN对话框,FALSE为SAVE AS对话框 if(dlg.DoModal()==IDO ...