The example in this article describes how C # creates an IIS virtual directory. Share to everyone for your reference. The specific analysis is as follows:
DirectoryEntry is a great gift from. NET, his name we know his function--directory entrance. People who have used ADSI know that when we operate iis,winnt these, we also need to provide their path, and when we operate IIS, the path is in the format:
The code is as follows:
Iis://computername/service/website/directory
ComputerName: That is, the name of the server to operate, can be the name can also be IP, often used is the localhost
Service: That is, the operation of the server, IIS has the Web, also has FTP, as well as SMTP these services, we mainly operate IIS Web functions, so here is "w3svc", if it is ftp should be "MSFTPSVC"
WebSite: An IIS service can include many sites, which are used to set up operations for the site. His value is a number, the default is 1, the default site, and if there are other, then start from 1 and so on.
Directory: Needless to say, the directory name of the operation, the general top-level directory of a site is "ROOT", and other directories are his children (child).
First we get the top-level directory (root directory) of a site:
The code is as follows:
DirectoryEntry RootFolder = new DirectoryEntry ("Iis://localhost/w3svc/1/root");
If we create this object without an exception, it means that the directory is real.
Let's add a new virtual directory, such as "ASPCN":
?
1 2 3 4 |
DirectoryEntry Newvirdir = RootFolder. Children.add ("ASPCN", "IIsWebVirtualDir"); Newvirdir.invoke ("AppCreate", true); Newvirdir.commitchanges (); Rootfolder.commitchanges (); |
The idea of creating a directory is simple, in a subset of the root directory (RootFolder. Children) to add a record, which uses the Add method in the Directoryentries class, which returns a DirectoryEntry that represents the newly added directory, the first parameter is the name of the virtual directory, The second is the class name of the schema to indicate the type of directory we are adding. Then, using the DirectoryEntry invoke method, call the "AppCreate" method in ADSI to actually create the directory (it doesn't seem to take this step to create a directory success, but for the sake of insurance, let's use it), and finally, call the new, The CommitChanges method of the root directory to confirm this operation.
When creating a new directory, we can also assign values to the attributes of the directory, but my actual combat experience tells me that it is best not to do so, and that if you assign values at creation time, there will be many attributes that cannot be assigned success, such as an important path attribute that represents the real directory. Therefore, the flying knife suggested that it is best to create a directory, and then assign a value, that is, update the directory information.
Update virtual Directory
I am sure you are familiar with IIS, and understand some important settings in IIS, such as readable (AccessRead), writable (AccessWrite), executable (AccessExecute), and so on. These can be achieved by assigning values to the Properties collection of the DirectoryEntry property. Assignments can be done in two ways:
The first is to invoke the Add method of the Properties collection, such as:
The code is as follows:
Dir. properties["AccessRead"]. ADD (TRUE);
The second is to assign a value to the first index value:
The code is as follows:
Dir. properties["AccessRead"][0] = true;
Both of these methods are feasible. It depends on your liking.
We still have to determine the target to be assigned before we do the assignment: Here we use the Find method of the Directoryentries class, such as:
The code is as follows:
DirectoryEntry de = RootFolder. Children.find ("ASPCN", "IIsVirtualDir");
If we find it, we can assign a value. You must take a good look at the assignment, the virtual directory can be more than the attribute value, a lot of checks. : (Too much, flying knives I do not repeat, we go to Microsoft's site to check:)
More commonly used are:
Accessread,accesswrite,accessexecute,accessscript,defaultdoc,enabledefaultdoc,path
Delete virtual directory
The way to delete a virtual directory is also simple: Find the virtual directory you want to delete, and then call the AppDelete method.
?
1 2 3 |
DirectoryEntry de = RootFolder. Children.find ("ASPCN", "IIsVirtualDir"); De. Invoke ("AppDelete", true); Rootfolder.commitchanges (); |
Another way is to invoke the Delete method of the root directory.
?
1 2 3 4 5 6 |
Object[] paras = new object[2]; Paras[0] = "IIsWebVirtualDir"; Represents the operation of the virtual directory paras[1] = "ASPCN"; RootFolder. Invoke ("Delete", paras); Rootfolder.commitchanges (); System.DirectoryServices.DirectoryEntries |
IIS creates a virtual directory
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148-149 150 151 152 153 154 155 156 157 158 159 |
Using System; Using System.Collections.Generic; Using System.Text; Using System.DirectoryServices; Namespace Install_iis {class Iismanager {public Iismanager () {}///<summary>///create virtual directory///</summary>/// <param name= "WebSite" > Server site name </param>///<param name= "vdirname" > virtual directory name </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; Determine IIS version//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//Gain Admin privileges//iisadmin=new System.DirectoryServices.DirectoryEntry ("iis://" +servername +/w3svc /"+ Websitenum +"/root "); if (IISAdmin = null) return "IIS not installed properly"; if (Iisadmin.children = null) return "IIS may not be started"; If we are not creating a root directory//If we cannot create a root//if (! RootDir) {////If the virtual directory already exists then delete it///foreach (System.directoryservic Es. DirectoryEntry V in Iisadmin.children) {if (V.name = = vdirname) {//Delete the specified virtual directoryIf it already exists try {iisadmin.invoke ("Delete", new string [] {v.schemaclassname, vdirname}); Iisadmin.commitchanges (); The catch (Exception ex) {Sret+=ex. message; }}////Create the virtual directory///if (! RootDir) {VDir = IISAdmin.Children.Add (vdirname, "IIsWebVirtualDir");} else {VDir = IISAdmin;}///Make it a web App Lication//Create a Web application//vdir.properties["path"][0] = path; Sets the physical path to the virtual directory if (iisundernt) {Vdir.invoke ("AppCreate", false);} else {Vdir.invoke ("AppCreate", 1);}////Setup the VDir//Set virtual directory//vdir.properties["AccessRead"][0] = Chkread; Set Read Permissions vdir.properties["AccessExecute"][0] = Chkexecute; Set execution Permissions vdir.properties["AccessWrite"][0] = chkwrite; Set Write permission vdir.properties["AccessScript"][0] = Chkscript; Execute permission vdir.properties["DefaultDoc"][0] = "index.asp,default.aspx";/set default document, multiple values in the middle with comma split vdir.properties[" AppFriendlyName "][0] = vdirname; Application name vdir.properties["AuthFlags"][0] = 0; Set the security of the directory, 0 means that anonymous access is not allowed, 1 is allowed, 3 is Basic authentication, and 7 is windows inherits the authentication vdir.properties["AUTHNTLM"][0] = Chkauth; vdir.properties["EnableDefaultDoc"][0] = true; vdir.properties["EnableDirBrowsing"][0] = false; NT doesn ' t support this property//NT format does not support this feature//if (! iisundernt) {vdir.properties["aspenableparentpaths"][0] = true;}////Set the changes//Set change//Vdir.commitchanges (); The following method is the way to get all the property names: foreach (PropertyValueCollection pvc in Vdir.properties) {Console.WriteLine (PVC). PropertyName); sret+= "VRoot" +vdirname + "created!"; return sret; #region Properties public string ServerName {get {_servername;} set {_servername = value;}} #endregion Publ IC static string virdirschemaname = "IIsWebVirtualDir"; #region Private members private string _servername; #endregion}} |
Test by:
The code is as follows:
MessageBox.Show (New Iismanager (). Createvdir ("localhost", "Ietm", "C:myweb", False, True, False, False, True, False, 1, "localhost"));
This I have put into the project to use, can be assured of use.
I hope this article will help you with the C # program.