Manage IIS using ASP.NET

zhaozj2021-02-16  73

Use ASP.NET to operate IIS, you can achieve the creation of virtual directory, virtual site, delete virtual directory, list, and so on. The key: ASP.NET's user permissions are not enough to operate IIS, to grant a high-point permission. Method 1, Add this line in the web.config file , but there is risky. Method 2, use fake, I use this method, but there is risky, other methods, slightly

Using system.io; using system.directoryservices;

namespace com.todayisp.iismanager {///

/// Summary description for IISManager /// public class IISManager {private DirectoryEntry _iisServer;. // public int Counter = 10; // create a virtual directory limit Quantity

// Create a virtual directory public string createvdir (String Websitename, String Namedirectory, Int Counter) {

String SiteId = getSiteId (Websitename); if (siteid == null) return "error: This site does not exist."

_iisServer = new DirectoryEntry ( "IIS: // localhost / W3SVC /" SiteID); try {DirectoryEntry Web = _iisServer.Children.Find ( "Root", "IIsWebVirtualDir"); DirectoryEntry VD = Web.Children.Find (nameDirectory, "IisWebVirtualDir"); return "error: The virtual directory you want to build already exists."

INT thecount = 0; Foreach (DirectoryEntry B1 in folderroot.children) {thecount = thecount 1;}

// If the number of virtual directories does not exceed the specified quantity, it is created. If (Thecount

// If you don't have this directory, create a real directory DirectoryInfo Di = Directory.createdIRectory (ThePath);

DirectoryEntry newVirDir = folderRoot.Children.Add (nameDirectory, "IIsWebVirtualDir"); newVirDir.CommitChanges ();. // Set Properties newVirDir.Properties [ "AccessRead"] Add (true); newVirDir.Properties [ "Path"] Value. = ThePath; // Create a Application; Do not use invoke method // Save Changes newVirDir.CommitChanges (); folderRoot.CommitChanges (); _iisServer.CommitChanges (); newVirDir.Invoke ( "AppCreate", true); return " Successful: You have successfully created a virtual directory: " NameDirectory;} else {return" error: Your site's virtual directory is full of " ", so you can't create it again. ";}} catch (Exception e) {return "error:. failed attempts to enter the site" e.Message;}}} // remove virtual directories public string DelVirtualDirectory (string WebSiteName, string nameDirectory) {try {string SiteID = GetSiteID (WebSiteName); if ( SiteID == NULL) Return "error: This site does not exist."

DirectoryEntry deRoot = new DirectoryEntry ( "IIS: // localhost / W3SVC /" SiteID "/ ROOT"); try {DirectoryEntry deVDir = deRoot.Children.Find (nameDirectory, "IIsWebVirtualDir"); deRoot.RefreshCache (); deVDir = deRoot.Children.Find (nameDirectory, "IIsWebVirtualDir"); deVDir.Invoke ( "AppDelete", null); deRoot.Children.Remove (deVDir); deRoot.CommitChanges (); deRoot.Close (); return "successful: Delete virtual directory " NameDirectory " success! ";} Catch {return" error: This virtual directory does not exist. ";}}} Catch (exception e) {return" error: Deleting the directory failed. }

// Find the corresponding virtual site public string GetSiteID (string WebSiteName) {DirectoryEntry root = new DirectoryEntry. ( "IIS: // localhost / W3SVC"); try {string SiteID = null; string hostname; foreach (DirectoryEntry bb in root. Children) {Try {// IF (BB.Name.trim ()) <0) Continue; PropertyValueCollection PVC = Bb.Properties ["ServerBindings"]; string [] srvbindings = (String) PVC [0 ]). Split (new char [] {':'}); hostname = srvbindings [2] .trim ();

// Judgment, can be used for hostname // if (Websitename == bb.properties ["servercomment"]. Value.tostring ()) siteid = bb.name; if (Websitename == Hostname) siteId = bb.name; / / Clean variable hostname = "";} catch {}} if (siteid == null) Return null; return siteId;} catCh} catCh}

// virtual directory rename public bool RenameVirtualDirectory (string WebSiteName, string nameDirectory, string NewName) {try {DirectoryEntry root = new DirectoryEntry ( "IIS: // localhost / W3SVC"); string SiteID = ""; string hostname; foreach ( DirectoryEntry BB in root.children) {Try {// IF (BB.Name.trim ()) <0) Continue; PropertyValueCollection PVC = Bb.Properties ["Serverbindings"]; string [] srvbindings = STRING) PVC [0]). Split (new char [] {':'}; hostname = srvbindings [2] .trim (); if (Hostname.equals ("))) Hostname =" localhost ";

// Judgment, can be used for hostname // if (Websitename == bb.properties ["servercomment"]. Value.tostring ()) siteid = bb.name; if (Websitename == Hostname) siteId = bb.name; / / Clear Variable hostname = "";} catch {}} DirectoryEntry deRoot = new DirectoryEntry ( "IIS: // localhost / W3SVC /" SiteID "/ ROOT"); DirectoryEntry deVDir = deRoot.Children.Find (nameDirectory, " IIsWebVirtualDir "); deRoot.RefreshCache (); deVDir = deRoot.Children.Find (nameDirectory," IIsWebVirtualDir "); deVDir.Rename (" yeah "); deRoot.CommitChanges (); deRoot.Close (); return true;} Catch {Return False;}}

// Create a virtual site, you need to enter "Site Name", "Site Root Category Call", "Port". Public Bool CreateWebsite (String Sitename, String Path, String Port) {DirectoryEntry Root = New DirectoryEntry (" IIS: // localhost / w3svc "); // Find unused value for new web site int sitei = 1; foreach (directoryEntry EF in root.children) {if (Ef.schemaclassName ==" IisWebserver ") {int id = Convert.TOINT32 (EF.NAME); if (id> = siteid) {siteid = ID 1;}}} // create Web site directoryentry site = (DirectoryEntry) root.invoke ("Create", "IisWeb Server", SiteID ); Site.Invoke ("Put", "Servercomment", SiteName); Site.Invoke ("Put", "Keytype", "IisWebserver"); Site.Invoke ("Put", "Serverbindings", ":" Port ":"); Site.Invoke ("Put", "Serverstate", 2); Site.Invoke ("Put", "FrontPageWeb", 1); Site.Invoke ("Put", "Defaultdoc", " Default.aspx "); Site.Invoke (" Put "," SecureBindings ",": 443: "); Site.Invoke (" Put "," ServerAutostart ", 1); Site.Invoke (" PUT "," Serversize ", 1); site.Invoke ( "SetInfo"); // Create application virtual DirectoryEntry siteVDir = site.Children.Add ( "Root", "IISWebVirtualDir"); siteVDir.Properties [ "AppIsolated"] [0] = 2; siteVDir.Properties [ "Path"] [0] = path; Sitevdir.Properties ["accessflags"] [0] = 513; Sitevdir.Properties ["frontPageWeb"] [0] = 1; Sitevdir.Properties ["approot"] [0] = "

/ LM / W3SVC / " SiteID " / root "; Sitevdir.Properties [" AppFriendlyName "] [0] =" root "; sitevdir.commitchanges (); site.commitchanges (); return true;} catch {return true; }} // public string DirAll (string WebSiteName, string VDirName, string Path) virtual directory virtual site list {DirectoryEntry root = new DirectoryEntry ( "IIS: // localhost / W3SVC"); string SiteID = ""; string hostname ; foreach (DirectoryEntry bb in root.Children) {try {// if (Convert.ToInt32 (bb.Name.Trim ()) <0) continue; PropertyValueCollection pvc = bb.Properties [ "ServerBindings"]; String [] srvBindings = (String) PVC [0]). Split (new char [] {':'}; hostname = srvbindings [2] .trim (); if (Hostname.Equals (")) Hostname =" LocalHost " ;

// Judgment, can be used for hostname // if (Websitename == bb.properties ["servercomment"]. Value.tostring ()) siteid = bb.name; if (Websitename == Hostname) siteId = bb.name; / / CLEAR VARIABLE HOSTNAME = ""} catch {}}

DirectoryEntry Deroot = New DirectoryEntry ("IIS: // localhost / W3SVC /" SiteID "/ root");

IF (vDirName == null) {string realpath = deroot.properties ["path"]. value.toTString (); string vpath = deroot.path; try {string alldir = "; string cc ="
";

Foreach (DirectoryEntry a in derOot.children) {alldir = alldir " " a.name "
" DirectoryInfo di = new DirectoryInfo (realPath); DirectoryInfo [] subdirectoryEntries = di.GetDirectories (); foreach (DirectoryInfo subdirectory in subdirectoryEntries) {cc = cc subdirectory.Name "
";} return AllDir;} catch {} Return "End";} else {DirectoryEntry Subroot = deeroot.children.find (vDirname, "IisWebVirtualdir); string realpath = subroot.properties [" path "]. value.toTString () " // " PATH " // "; try {string cc ="
"; string filename ="
";

DirectoryInfo di = new DirectoryInfo (RealPath); DirectoryInfo [] subdirectoryEntries = di.GetDirectories (); foreach (DirectoryInfo subdirectory in subdirectoryEntries) {cc = cc "?