Create a virtual directory (including scripting language) in. NET (in IIS)

Source: Internet
Author: User
Tags error code iis iis administration net
iis| Create | script | virtual directory

Two languages create virtual directories in IIS

Create a virtual directory in IIS with. NET
Use. NET directory service, you can access the settings of IIS, and adding virtual directories is actually creating a DirectoryEntry
The complexity lies in the DirectoryEntry attributes, in fact, the virtual purpose of some configuration, such as permissions, whether to log, application name, etc.
There are so many properties, and documents are not easy to find
Refer to the MSDN below for more information
help://MS. Msdnqtr.2004jul.1033/iissdk/iis/configuring_properties_in_the_iis_user_interface.htm

code example:

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 being 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 ();

//************************************************************
Create a virtual directory in Windows scripting language.

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 Li Ke it re-mapped for this sample? ", 0," Remap Virtual Directory? ", 4 +) ' 4 = yesno & = 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 ' in-process a ' Pplication.
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", ' = Stop
Wscript.Quit
End If

' Get the ' name of 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", ' = Stop
Wscript.Quit
Else
res = vdirname & "has been created at" & VbCrLf & Vdirpath
Shell. Popup Res, 0, "All done", "the" = 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", ' = Stop
End If

End Sub

End



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.