日期:2010-06-11 浏览次数:20501 次
用.net在IIS中创建虚拟目录
使用.NET的目录服务就可以访问IIS的设置,添加虚拟目录其实就是创建一个DirectoryEntry
复杂的在于DirectoryEntry的属性,其实就是虚拟目的一些配置,比如,权限,是否要log,应用程序名等
Properties非常多,而且文档不太好找
详细可以参考MSDN以下内容
help://MS.MSDNQTR.2004JUL.1033/iissdk/iis/configuring_properties_in_the_iis_user_interface.htm
代码示例:
const String constIISWebSiteRoot = "IIS://localhost/W3SVC/1/ROOT";
DirectoryEntry root = new DirectoryEntry(constIISWebSiteRoot);
DirectoryEntry entry = new DirectoryEntry(constIISWebSiteRoot + "/" + virtualDirName);
DirectoryEntry tbEntry = root.Children.Add(virtualDirName, "IIsWebVirtualDir");
//must be end with a '\'
tbEntry.Properties["Path"][0] = virtualDirPath;
tbEntry.Invoke("AppCreate",true);
tbEntry.Properties["AccessRead"][0] = true;
tbEntry.Properties["ContentIndexed"][0] = false;
tbEntry.Properties["DefaultDoc"][0] = "index.asp";
tbEntry.Properties["AppFriendlyName"][0] = virtualDirName;
tbEntry.Properties["AppIsolated"][0] = 2;
tbEntry.Properties["AccessScript"][0] = true;
tbEntry.Properties["DontLog"][0] = true;
tbEntry.CommitChanges();
//************************************************************
用windows 脚本语言创建虚拟目录.
Set shell = Wscript.CreateObject( "WScript.Shell" )
If Wscript.Arguments.Count < 2 Then
usage = "USAGE: theVbs virtual_directory_name directory_location_to_map"
WScript.Echo usage
Wscript.Quit
End If
vDirName = Wscript.Arguments(0)
vDirPath = Wscript.Arguments(1)
' Get the name of the current directory
Set fso = WScript.CreateObject( "Scripting.FileSystemObject" )
vDirPath = fso.GetFolder( vDirPath ).Path
' Does this IIS application already exist in the metabase?
On Error Resume Next
Set objIIS = GetObject( "IIS://localhost/W3SVC/1/Root/" & vDirName )
If Err.Number = 0 Then
result = shell.Popup( "A virtual directory named " & vDirName & " already exists. " & vbCrLf & vbCrLf & "Would you like it re-mapped for this sample?", 0 ,"Remap Virtual Directory?", 4 + 32 )' 4 = YesNo & 32 = Question
If result = 6 Then ' 6 = Yes
DeleteVirtualDirectory vDirName
Else
WScript.Quit
End If
End If
'Using IIS Administration object , turn on script/execute permissions and define the virtual directory as an 'in-process application.
Set objIIS = GetObject( "IIS://localhost/W3SVC/1/Root" )
Set vDirObj = objIIS.Create( "IISWebVirtualDir", vDirName )
vDirObj.Path = vDirPath
vDirObj.AuthNTLM = True
vDirObj.AccessRead = True
vDirObj.AccessWrite = True
vDirObj.AccessScript = True
vDirObj.AccessExecute = True
vDirObj.AuthAnonymous = True
'vDirObj.AnonymousUserName = owner
vDirObj.AnonymousPasswordSync = True
vDirObj.AppCreate True
vDirObj.SetInfo
If Err.Number > 0 Then
shell.Popup Err.Description, 0, "Error", 16 ' 16 = Stop
WScript.Quit
End If
' Get the name of the account for the anonymous user in IIS
owner = vDirObj.AnonymousUserName
' Change necessary folder permissions using CACLS.exe
aclCmd = "cmd /c echo y| CACLS "
aclCmd = aclCmd & """" & vDirPath & """"
aclCmd = aclCmd & " /E /G " & owner & ":C"
rtc = shell.Run( aclCmd , 0, True )
aclCmd = "cmd /c echo y| CACLS "
aclCmd = aclCmd & """" & vDirPath & """"
aclCmd = aclCmd & " /E /G ""VS Developers"":C"
rtc = shell.Run( aclCmd , 0, True )
If Err.Number > 0 Then
shell.Popup Err.Description, 0,