如果 i = 0则会抛出IndexOutOfRangeException错误...这二点可以注意一下.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Security.Policy;
using System.Security;
using System.Collections;
namespace TayeWorks.TEMP
{
[RunInstaller(true)]
public partial class Installer : System.Configuration.Install.Installer
{
public Installer()
{
InitializeComponent();
}
private string codeGroupName = "TayeWorks TEMP";
private PolicyLevel _currentPolicyLevel = null;
protected PolicyLevel CurrentPolicyLevel
{
get
{
if (_currentPolicyLevel == null)
{
IEnumerator en = SecurityManager.PolicyHierarchy();
while (en.MoveNext())
{
PolicyLevel pl = en.Current as PolicyLevel;
if (pl != null)
{
if (pl.Label == "User")
{
_currentPolicyLevel = pl;
break;
}
}
}
if (_currentPolicyLevel == null)
throw new ApplicationException("未找到相关PolicyLevel");
}
return _currentPolicyLevel;
}
}
protected override void OnAfterInstall(System.Collections.IDictionary savedState)
{
base.OnAfterInstall(savedState);
try
{
//生成FullTrust
PermissionSet permissionSet = new NamedPermissionSet("FullTrust");
//安装目录
string assemblyPath = this.Context.Parameters["assemblypath"];
string installDirectory =
assemblyPath.Substring(0, assemblyPath.LastIndexOf("\\"));
if (!installDirectory.EndsWith(@"\"))
installDirectory += @"\";
installDirectory += "*";
//生成UrlMembershipCondition
IMembershipCondition membershipCondition = new UrlMembershipCondition(installDirectory);
//生成CodeGroup添加相关权限
PolicyStatement policyStatement = new PolicyStatement(permissionSet);
CodeGroup codeGroup = new UnionCodeGroup(membershipCondition, policyStatement);
codeGroup.Description = "TayeWorks.TEMP";
codeGroup.Name = codeGroupName;
//删除以前可能的存在
foreach( CodeGroup cg in CurrentPolicyLevel.RootCodeGroup.Children)
{
if (cg.Name == codeGroup.Name)
{
CurrentPolicyLevel.RootCodeGroup.RemoveChild(cg);
}
}
//添加保存
CurrentPolicyLevel.RootCodeGroup.AddChild(codeGroup);
SecurityManager.SavePolicy();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}
}
protected override void OnAfterUninstall(System.Collections.IDictionary savedState)
{
base.OnAfterUninstall(savedState);
//删除存在CodeGroup
foreach (CodeGroup cg in CurrentPolicyLevel.RootCodeGroup.Children)
{
if (cg.Name == codeGroupName)
{
CurrentPolicyLevel.RootCodeGroup.RemoveChild(cg);
SecurityManager.SavePolicy();
}
}
}
}
}
这样才可以加载正确..
2005-12-30年底了..新年..