Operate the virtual directory of IIS in ASP.NET (C #)

xiaoxiao2021-03-06  14

Creating a virtual directory DirectoryEntry is a big gift for us. His name We know his function - the entrance of the catalog. People who have used ADSI know that when operating IIS, Winnt, we also need to provide their PATH. When you operate IIS, this path format is: IIS: // computername / service / Website / Directory Computename: That's a server Name, can be named or IP, often used by localhost service: the server, IIS has web, there is FTP, and SMTP these services, we mainly operate IIS's web feature, so "here is" W3SVC, if FTP should be "MSFTPSVC" Website: A lot of sites can be included in an IIS service, which is used to set up a site. His value is a number, default is 1, indicating the default site, if there is anything, then push from 1. Directory: Don't say, the directory name of the operation, a site general top-level directory is "root", and other directory is his child (Child). First we get a top-level directory (root directory): DirectoryEntry Rootfolder = New DirectoryEntry ("IIS: // LocalHost / W3SVC / 1 / root"); if we create this object, it is true that this directory is true. existing. Let's add a new virtual directory, such as what we have to add "ASPCN": DirectoryEntry newvirdir = rootfolder.children.add ("ASPCN", "IisWebVirtualDir"); NewVirdir.Invoke ("AppCreate", true); newvirdir. Commitfolder.commitchanges (); Create a directory's ideas, that is, add a record in the subset of the root directory (rootfolder.children), use the add method in the DirectoryEntries class, which is returned. A DirectoryEntry, indicating the newly joined directory, the first parameter is the name of the virtual directory, the second is the class name of Schema to indicate the type of directory we join. Then use DirectoryEntry's Invoke method, call the "AppCreate" method in ADSI to truly create (seek not going this step can also create a directory success, but for insurance, everyone still uses it), and finally call new, The COMMITCHANGES method of the root directory confirms this operation. When you create a new directory, we can also assign a value to this directory at the same time, but my actual experience tells me that it is best not to do this. If you create it, you will have a lot of attributes that you can't assign a value, such as important representation. The PATH property of the directory. So the flying knife suggests that everyone is best to create a directory, then assign the value, that is, update the directory information.

Updating the virtual directory believes that everyone is more familiar with IIS, understands some important settings in IIS, such as readable (AccessWrite), executable (AccessExecute), etc. These can be implemented by assigning the DirectoryEntry's Properties attribute set. Assignment can be completed in two ways: the first is the add method of calling the Properties collection, such as Dir.Properties ["AccessRead"]. Add (true); the second is to assign the first index value: DIR. Properties [AccessRead "] [0] = true; these two methods are feasible. Specifically, you have to look at your preference. We still have to determine the target to assign values ​​before making assignment :) Here we use the DirectoryEntries class Find method, such as: DirectoryEntry de = rootfolder.children.Find ("aspcn", "Iisvirtualdir); found, we It can be assigned. Be sure to look at it when you assign a value, the attribute value of the virtual directory can be super, and a lot is checked. . :( Too much, I don't repeat, everyone go to Microsoft's site :) Compare commonly: AccessRead, AccessWrite, AccessExecute, AccessScript, defaultdoc, enabledeDefaultdoc, Path Delete virtual directory delete virtual directory methods Very simple, that is to find the virtual directory you want to delete, then call the AppDelete method. DirectoryEntry de = rootfolder.children.Find ("ASPCN", "IisvirtualDir"); de.invoke ("appdelete"; rootfolder.commitchanges (); there is a way to call the root directory of the DELETE method. Object [] Paras = New Object [2]; Paras [0] = "IisWebVirtualDir"; // Indicates that the operation is the virtual directory Paras [1] = "aspcn"; rootfolder.invoke ("delete", paras); rootfolder. Commitchange (); system.Object System.directoryServices.directoryEntries

IIS creation virtual directory http://www.eggheadcafe.com/articles/20040112.asp

Using system;

Using System.directoryServices;

Namespace Iismgraddin

{

///

/// Summary Description for Iismanager.

///

Public Class Iismanager

{

///

/// conntructor

///

///

/// default constructor Uses localhost as default server

/// public iiysmanager ()

{

}

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 Version of Iis

//

Iisschema = new system.directoryServices.directoryEntry ("IIS: //" ServerName "/ Schema / Appisolated");

II (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] = kkscript;

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

//

IF (! iiSundernt)

{

Vdir.properties ["aspenableparentpaths"] [0] = true;

}

//

// set the changes

//

Vdir.commitchanges ();

//

// Make it a web application

//

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;

}

}

#ndregion

Public static string virdirschemaname = "iiswebvirtualdir";

#Region Private MEMBERS

Private string_servername;

#ndregion

}

}

转载请注明原文地址:https://www.9cbs.com/read-48156.html

New Post(0)