复制代码 代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.DirectoryServices;
using System.Diagnostics;
using System.Windows.Forms;
using System.Security.AccessControl;
using System.IO;
namespace InstallClassLibrary
{
[RunInstaller(true)]
public partial class WebInstaller : Installer
{
public WebInstaller()
{
InitializeComponent();
}
public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);
CreateVirtualDir();//以下代码为更改网站指定目录权限
DirectoryInfo di = new DirectoryInfo("d://yourpath//xml");
if((di.Attributes&FileAttributes.ReadOnly)!=0)
di.Attributes=FileAttributes.Normal;
DirectorySecurity ds=di.GetAccessControl();
ds.AddAccessRule(new FileSystemAccessRule("NETWORK SERVICE",FileSystemRights.Modify,InheritanceFlags.ObjectInherit|InheritanceFlags.ContainerInherit,
PropagationFlags.None,AccessControlType.Allow));
di.SetAccessControl(ds);
//
}
void CreateVirtualDir()
{
try
{
DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC/1/root");
DirectoryEntry newRoot = root.Children.Add("virtualName", root.SchemaClassName);
newRoot.Properties["Path"][0] = "d://yourpath"; //this.Context.Parameters["targetdir"];
newRoot.Properties["AppIsolated"][0] = 2; // 值 0 表示应用程序在进程内运行,值 1 表示进程外,值 2 表示进程池
newRoot.Properties["AccessScript"][0] = true; // 可执行脚本
newRoot.Invoke("AppCreate", true);
newRoot.Properties["DefaultDoc"][0] = "login.aspx";//设置起始页
newRoot.Properties["AppFriendlyName"][0] = "applicationName"; // 应用程序名
newRoot.CommitChanges();
root.CommitChanges();
}
catch (Exception ee)
{
MessageBox.Show("虚拟目录创建失败!您可以手动创建! " + ee.Message + ";" + ee.Source + ";" + ee.TargetSite + ";" + ee.InnerException + ";" + ee.StackTrace, "Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
}
}
}
}
新闻热点
疑难解答
图片精选