在线等!!!急!!!在线给分!!! 我现在做一个网站的安装程序,现在碰到个问题,我怎么通过c#代码设置虚拟目录并将目录属性设置成可读可写?本人用的开发环境是vs2005,.net framework 2.0,由于项目时间较紧,所以希望可以尽快得到答复,本人不吝啬分数哒!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 找了篇文章 看看有帮助吗DirectoryEntry是.Net给我们的一大礼物,他的名字我们就知道他的功能--目录入口。使用过ADSI的人都知道操作IIS,WinNT这些时,我们还需要提供他们的Path,操作IIS时,这个Path的格式为: IIS://ComputerName/Service/Website/Directory ComputerName:即操作的服务器的名字,可以是名字也可以是IP,经常用的就是localhost Service:即操作的服务器,IIS中有Web,也有FTP,还有SMTP这些服务,我们主要是操作IIS的Web功能,因此此处就是"W3SVC",如果是FTP则应是"MSFTPSVC" WebSite:一个IIS服务中可以包括很多的站点,这个就用于设置操作的站点。他的值是一个数字,默认是1,表示缺省站点,如果有其它,则从1开始依次类推。 Directory:不用说,即操作的目录名称,一个站点一般顶层目录为"ROOT",其它目录则是他的孩子(Child)。 首先我们获取一个站点的顶层目录(根目录): DirectoryEntry rootfolder = new DirectoryEntry("IIS://localhost/W3SVC/1/ROOT"); 如果我们创建这个对象是没有发生异常,则表示这个目录是真实存在的。 下面我们来添加新的虚拟目录,比如我们要加的是"Aspcn": DirectoryEntry newVirDir = rootfolder.Children.Add("Aspcn","IIsWebVirtualDir"); newVirDir.Invoke("AppCreate",true); newVirDir.CommitChanges(); rootfolder.CommitChanges(); 创建目录的思路很简单,即在根目录的子集(rootfolder.Children)中再添加一条记录,这里使用的是DirectoryEntries类中的Add方法,它返回的是一个DirectoryEntry,表示新加入的目录,第一个参数是虚拟目录的名字,第二个则是Schema的类名以表明我们加入的目录类型。然后再使用DirectoryEntry的Invoke方法,调用ADSI中的"AppCreate"方法将目录真正创建(似乎不走这一步也可以创建目录成功,但是为了保险起见,大家还是用吧),最后便是依次调用新、根目录的CommitChanges方法,确认此次操作。 在创建新目录时,我们也可以同时给这个目录的属性赋值,但是我的实战经验告诉我,最好不要这样做,如果创建时就赋值,将有很多属性不能赋值成功,比如重要的表示真实目录的Path属性。因此飞刀建议大家最好是先创建目录,然后再赋值,即更新目录信息。 更新虚拟目录 相信大家对IIS都比较熟悉,了解IIS中一些重要的设置,如可读(AccessRead)、可写(AccessWrite)、可执行(AccessExecute)等。这些都可通过对DirectoryEntry的Properties属性集合的赋值来实现。赋值可以通过两种方式来完成: 第一种是调用Properties集合的Add方法,如: dir.Properties["AccessRead"].Add(true); 第二种是对第一个索引值赋值: dir.Properties["AccessRead"][0] = true; 这两种方法都是可行的。具体是要看你的喜好了。 在进行赋值之前我们还是要确定要要赋值的目标吧:)这里我们使用DirectoryEntries类的Find方法,如: DirectoryEntry de = rootfolder.Children.Find("Aspcn","IIsVirtualDir"); 找到了,我们就可以赋值了。赋值时一定要好好看看啊,虚拟目录的属性值可以超多,一查一大堆。。:(太多了,飞刀我也不重复了,大家去微软的站点上查:) 比较常用的有:AccessRead,AccessWrite,AccessExecute,AccessScript,DefaultDoc,EnableDefaultDoc,Path 删除虚拟目录 删除虚拟目录的方法也很简单,就是找到你要删除的虚拟目录,然后调用AppDelete方法。 DirectoryEntry de = rootfolder.Children.Find("Aspcn","IIsVirtualDir"); de.Invoke("AppDelete",true); rootfolder.CommitChanges(); 还有一种方法,就是调用Root目录的Delete方法。 object[] paras = new object[2]; paras[0] = "IIsWebVirtualDir"; //表示操作的是虚拟目录 paras[1] = "Aspcn"; rootfolder.Invoke("Delete",paras); rootfolder.CommitChanges(); System.Object System.DirectoryServices.DirectoryEntriesIIs创建虚拟目录 http://www.eggheadcafe.com/articles/20040112.aspusing System;using System.Collections.Generic;using System.Text;using System.DirectoryServices;namespace Install_IIS{ class IISManager { public IISManager() { } /// <summary> /// 创建虚拟目录 /// </summary> /// <param name="WebSite">服务器站点名称</param> /// <param name="VDirName">虚拟目录名称</param> /// <param name="Path"></param> /// <param name="RootDir"></param> /// <param name="chkRead"></param> /// <param name="chkWrite"></param> /// <param name="chkExecute"></param> /// <param name="chkScript"></param> /// <param name="chkAuth"></param> /// <param name="webSiteNum">1</param> /// <param name="serverName">localhost</param> /// <returns></returns> public string CreateVDir(string WebSite,string VDirName, string Path, bool RootDir, bool chkRead,bool chkWrite, bool chkExecute, bool chkScript, bool chkAuth, int webSiteNum, string serverName) { string sRet=String.Empty; System.DirectoryServices.DirectoryEntry IISSchema; System.DirectoryServices.DirectoryEntry IISAdmin; System.DirectoryServices.DirectoryEntry VDir; bool IISUnderNT; // // 确定IIS版本 // IISSchema = new System.DirectoryServices.DirectoryEntry("IIS://" + serverName + "/Schema/AppIsolated"); if(IISSchema.Properties["Syntax"].Value.ToString().ToUpper()=="BOOLEAN") IISUnderNT=true; else IISUnderNT=false; IISSchema.Dispose(); // // Get the admin object // 获得管理权限 // IISAdmin=new System.DirectoryServices.DirectoryEntry("IIS://" +serverName +"/W3SVC/" + webSiteNum + "/Root"); // // If we're not creating a root directory // 如果我们不能创建一个根目录 // if (!RootDir) { // // If the virtual directory already exists then delete it // 如果虚拟目录已经存在则删除 // foreach(System.DirectoryServices.DirectoryEntry v in IISAdmin.Children) { if (v.Name == VDirName) { // Delete the specified virtual directory if it already exists try { IISAdmin.Invoke("Delete", new string [] { v.SchemaClassName, VDirName }); IISAdmin.CommitChanges(); } catch(Exception ex) { sRet+=ex.Message; } } } } // // Create the virtual directory // 创建一个虚拟目录 // if (!RootDir) { VDir = IISAdmin.Children.Add(VDirName, "IIsWebVirtualDir"); } else { VDir = IISAdmin; } // // Setup the VDir // 安装虚拟目录 // VDir.Properties["AccessRead"][0] = chkRead; VDir.Properties["AccessExecute"][0] = chkExecute; VDir.Properties["AccessWrite"][0] = chkWrite; VDir.Properties["AccessScript"][0] = chkScript; VDir.Properties["AuthNTLM"][0] = chkAuth; VDir.Properties["EnableDefaultDoc"][0] = true; VDir.Properties["EnableDirBrowsing"][0] = false; VDir.Properties["DefaultDoc"][0] = true; VDir.Properties["Path"][0] = Path; // // NT doesn't support this property // NT格式不支持这特性 // if (!IISUnderNT) { VDir.Properties["AspEnableParentPaths"][0] = true; } // // Set the changes // 设置改变 // VDir.CommitChanges(); // // Make it a web application // 创建一个web应用 // if (IISUnderNT) { VDir.Invoke("AppCreate", false); } else { VDir.Invoke("AppCreate", 1); } sRet+= "VRoot " +VDirName + " created!"; return sRet; } #region Properties public string ServerName { get { return _serverName; } set { _serverName = value; } } #endregion public static string VirDirSchemaName = "IIsWebVirtualDir"; #region Private Members private string _serverName; #endregion } } MessageBox.Show(new IISManager().CreateVDir("localhost", "V", "c:\\vv", false, true, false, false, true, false, 1, "localhost")); 網站中創建一個安裝類,重寫提交事件 [RunInstaller(true)] public partial class Installer1 : Installer { public Installer1() { InitializeComponent(); } public override void Commit(System.Collections.IDictionary savedState) { base.Commit(savedState); //設置權限 string folderpath="自己從安裝信息中取"; System.Diagnostics.Process.Start("cacls.exe", "\"" + folderpath + "\" /G everyone:F /T /E"); } }在安裝項目中新增一個自定義的提交操作,選擇為網站的主輸出。OK 關於權限的設置,請自己在命令行下輸入 cacls.exe \help 查看 求助一小段代码分析!一个测试题吧~ C#中有点难度的XML解析,高手请进 sqlserver2005 中的连接问题!!!急,请帮忙 如何用ListView读取ini文件 不是问问题,希望大家给发个文件,谢谢! 请问如何RichTextBox中的某些字是红色(或者其他颜色) 本人估价 XML转List<T> 100分!!!!求一个关于算法方面的问题 求反编译工具 vs2008怎么将网站项目发布成只有一个dll,就像原来vs2005装个插件就能发布成一个dll 关于拖拽下载功能
IIS://ComputerName/Service/Website/Directory
ComputerName:即操作的服务器的名字,可以是名字也可以是IP,经常用的就是localhost
Service:即操作的服务器,IIS中有Web,也有FTP,还有SMTP这些服务,我们主要是操作IIS的Web功能,因此此处就是"W3SVC",如果是FTP则应是"MSFTPSVC"
WebSite:一个IIS服务中可以包括很多的站点,这个就用于设置操作的站点。他的值是一个数字,默认是1,表示缺省站点,如果有其它,则从1开始依次类推。
Directory:不用说,即操作的目录名称,一个站点一般顶层目录为"ROOT",其它目录则是他的孩子(Child)。
首先我们获取一个站点的顶层目录(根目录):
DirectoryEntry rootfolder = new DirectoryEntry("IIS://localhost/W3SVC/1/ROOT");
如果我们创建这个对象是没有发生异常,则表示这个目录是真实存在的。
下面我们来添加新的虚拟目录,比如我们要加的是"Aspcn":
DirectoryEntry newVirDir = rootfolder.Children.Add("Aspcn","IIsWebVirtualDir");
newVirDir.Invoke("AppCreate",true);
newVirDir.CommitChanges();
rootfolder.CommitChanges();
创建目录的思路很简单,即在根目录的子集(rootfolder.Children)中再添加一条记录,这里使用的是DirectoryEntries类中的Add方法,它返回的是一个DirectoryEntry,表示新加入的目录,第一个参数是虚拟目录的名字,第二个则是Schema的类名以表明我们加入的目录类型。然后再使用DirectoryEntry的Invoke方法,调用ADSI中的"AppCreate"方法将目录真正创建(似乎不走这一步也可以创建目录成功,但是为了保险起见,大家还是用吧),最后便是依次调用新、根目录的CommitChanges方法,确认此次操作。
在创建新目录时,我们也可以同时给这个目录的属性赋值,但是我的实战经验告诉我,最好不要这样做,如果创建时就赋值,将有很多属性不能赋值成功,比如重要的表示真实目录的Path属性。因此飞刀建议大家最好是先创建目录,然后再赋值,即更新目录信息。
更新虚拟目录
相信大家对IIS都比较熟悉,了解IIS中一些重要的设置,如可读(AccessRead)、可写(AccessWrite)、可执行(AccessExecute)等。这些都可通过对DirectoryEntry的Properties属性集合的赋值来实现。赋值可以通过两种方式来完成:
第一种是调用Properties集合的Add方法,如:
dir.Properties["AccessRead"].Add(true);
第二种是对第一个索引值赋值:
dir.Properties["AccessRead"][0] = true;
这两种方法都是可行的。具体是要看你的喜好了。
在进行赋值之前我们还是要确定要要赋值的目标吧:)这里我们使用DirectoryEntries类的Find方法,如:
DirectoryEntry de = rootfolder.Children.Find("Aspcn","IIsVirtualDir");
找到了,我们就可以赋值了。赋值时一定要好好看看啊,虚拟目录的属性值可以超多,一查一大堆。。:(太多了,飞刀我也不重复了,大家去微软的站点上查:)
比较常用的有:AccessRead,AccessWrite,AccessExecute,AccessScript,DefaultDoc,EnableDefaultDoc,Path
删除虚拟目录
删除虚拟目录的方法也很简单,就是找到你要删除的虚拟目录,然后调用AppDelete方法。
DirectoryEntry de = rootfolder.Children.Find("Aspcn","IIsVirtualDir");
de.Invoke("AppDelete",true);
rootfolder.CommitChanges();
还有一种方法,就是调用Root目录的Delete方法。
object[] paras = new object[2];
paras[0] = "IIsWebVirtualDir"; //表示操作的是虚拟目录
paras[1] = "Aspcn";
rootfolder.Invoke("Delete",paras);
rootfolder.CommitChanges(); System.Object
System.DirectoryServices.DirectoryEntriesIIs创建虚拟目录
http://www.eggheadcafe.com/articles/20040112.aspusing System;using System.Collections.Generic;using System.Text;using System.DirectoryServices;
namespace Install_IIS{ class IISManager { public IISManager() { } /// <summary> /// 创建虚拟目录 /// </summary> /// <param name="WebSite">服务器站点名称</param> /// <param name="VDirName">虚拟目录名称</param> /// <param name="Path"></param> /// <param name="RootDir"></param> /// <param name="chkRead"></param> /// <param name="chkWrite"></param> /// <param name="chkExecute"></param> /// <param name="chkScript"></param> /// <param name="chkAuth"></param> /// <param name="webSiteNum">1</param> /// <param name="serverName">localhost</param> /// <returns></returns> public string CreateVDir(string WebSite,string VDirName, string Path, bool RootDir, bool chkRead,bool chkWrite, bool chkExecute, bool chkScript, bool chkAuth, int webSiteNum, string serverName) { string sRet=String.Empty; System.DirectoryServices.DirectoryEntry IISSchema; System.DirectoryServices.DirectoryEntry IISAdmin; System.DirectoryServices.DirectoryEntry VDir; bool IISUnderNT;
// // 确定IIS版本 // IISSchema = new System.DirectoryServices.DirectoryEntry("IIS://" + serverName + "/Schema/AppIsolated"); if(IISSchema.Properties["Syntax"].Value.ToString().ToUpper()=="BOOLEAN") IISUnderNT=true; else IISUnderNT=false; IISSchema.Dispose();
// // Get the admin object // 获得管理权限 // IISAdmin=new System.DirectoryServices.DirectoryEntry("IIS://" +serverName +"/W3SVC/" + webSiteNum + "/Root");
// // If we're not creating a root directory // 如果我们不能创建一个根目录 // if (!RootDir) { // // If the virtual directory already exists then delete it // 如果虚拟目录已经存在则删除 //
foreach(System.DirectoryServices.DirectoryEntry v in IISAdmin.Children) { if (v.Name == VDirName) { // Delete the specified virtual directory if it already exists try { IISAdmin.Invoke("Delete", new string [] { v.SchemaClassName, VDirName }); IISAdmin.CommitChanges(); } catch(Exception ex) { sRet+=ex.Message; } } } }
// // Create the virtual directory // 创建一个虚拟目录 // if (!RootDir) { VDir = IISAdmin.Children.Add(VDirName, "IIsWebVirtualDir"); } else { VDir = IISAdmin; }
// // Setup the VDir // 安装虚拟目录 // VDir.Properties["AccessRead"][0] = chkRead; VDir.Properties["AccessExecute"][0] = chkExecute; VDir.Properties["AccessWrite"][0] = chkWrite; VDir.Properties["AccessScript"][0] = chkScript; VDir.Properties["AuthNTLM"][0] = chkAuth; VDir.Properties["EnableDefaultDoc"][0] = true; VDir.Properties["EnableDirBrowsing"][0] = false; VDir.Properties["DefaultDoc"][0] = true; VDir.Properties["Path"][0] = Path;
// // NT doesn't support this property // NT格式不支持这特性 // if (!IISUnderNT) { VDir.Properties["AspEnableParentPaths"][0] = true; }
// // Set the changes // 设置改变 // VDir.CommitChanges();
// // Make it a web application // 创建一个web应用 // if (IISUnderNT) { VDir.Invoke("AppCreate", false); } else { VDir.Invoke("AppCreate", 1); }
sRet+= "VRoot " +VDirName + " created!"; return sRet; }
#region Properties public string ServerName { get { return _serverName; } set { _serverName = value; } } #endregion
public static string VirDirSchemaName = "IIsWebVirtualDir";
#region Private Members private string _serverName;
#endregion } }
MessageBox.Show(new IISManager().CreateVDir("localhost", "V", "c:\\vv", false, true, false, false, true, false, 1, "localhost"));
public partial class Installer1 : Installer
{
public Installer1()
{
InitializeComponent();
}
public override void Commit(System.Collections.IDictionary savedState)
{
base.Commit(savedState);
//設置權限
string folderpath="自己從安裝信息中取";
System.Diagnostics.Process.Start("cacls.exe", "\"" + folderpath + "\" /G everyone:F /T /E");
}
}在安裝項目中新增一個自定義的提交操作,選擇為網站的主輸出。
OK