首页 > 编程 > .NET > 正文

用.net (在IIS中)创建虚拟目录(包括 脚本语言)

2024-07-10 13:11:51
字体:
来源:转载
供稿:网友
中国最大的web开发资源网站及技术社区,

两种语言在iis中创建虚拟目录

用.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, "error", 16 ' 16 = stop
wscript.quit
else
res = vdirname & " has been created at" & vbcrlf & vdirpath
shell.popup res, 0, "all done", 64 ' 64 = information
end if

sub deletevirtualdirectory( nameofvdir )

set iis = getobject("iis://localhost/w3svc/1/root")
iis.delete "iiswebvirtualdir", vdirname

if err.number <> 0 then

errorstring = "unable to delete exisiting virtual directory."

if err.description is nothing then
errorstring = errorstring & "error code: " & err.number
else
errorstring = errorstring & "description: " & err.description
end if

shell.popup errorstring, 0, "error", 16 ' 16 = stop
end if

end sub

//********************  end

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表