程序集、应用程序配置及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 ...
随机推荐
- Android Capture Android System Audio
项目需要获取播放视频的实时音量值,最简捷的方法是监听音频输出端,取得音频输出流,再进行转换. 调查时,首先找到这篇博客: http://blog.csdn.net/jinzhuojun/article ...
- 30个有关Python的小技巧
从我开始学习python的时候,我就开始自己总结一个python小技巧的集合.后来当我什么时候在Stack Overflow或者在某个开源软件里看到一段很酷代码的时候,我就很惊讶:原来还能这么做!,当 ...
- hadoop-1.2.0源码编译
以下为在CentOS-6.4下hadoop-1.2.0源码编译步骤. 1. 安装并且配置ant 下载ant,将ant目录下的bin文件夹加入到PATH变量中. 2. 安装git,安装autoconf, ...
- 【转】类中如何引用server.MapPath()
转至:http://blog.csdn.net/tangjianft/article/details/5357151 今天在写一个上传图片方法时遇到了两个问题:1.public string getI ...
- Linux下的paste合并命令详解
paste单词意思是粘贴.该命令主要用来将多个文件的内容合并,与cut命令完成的功能刚好相反. 粘贴两个不同来源的数据时,首先需将其分类,并确保两个文件行数相同.paste将按行将不同文件行信息放在一 ...
- ubuntu 64位系统下加速Android模拟器
安装KVM: sudo apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils 在Postfix Configurati ...
- STM32中的位带(bit-band)操作
转:http://blog.csdn.net/gaojinshan/article/details/11479929 //位带操作,实现51类似的GPIO控制功能 //具体实现思想,参考<< ...
- Array types are now written with the brackets around the element type
因为网站翻译的时候应该用的beta/beta2,而再beta4中就会出现问题,解决问题方案: var shopping: String[] = ["Eggs","Milk ...
- HDU 2859 Phalanx (dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2859 给你一个n*n的矩阵,问你最大的对称度是多少(左下右上为对称线) dp[i][j]表示i行j列元 ...
- 用ILMerge.exe合并dll
F:\ILMerge>ILMerge /targetplatform:v4,"C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319" / ...