Code to manipulate the virtual directory of IIS

Source: Internet
Author: User
Tags foreach define bool contains iis connect return access
iis| virtual Directory

Find your own experiment online and sort it out:

The entire background file is as follows:

Using System;
Using System.Data;
Using System.DirectoryServices;
Using System.Collections;
Using System.Threading;
Namespace Aspcn.management
{
<summary>
Summary description of the Iismanager.
</summary>
public class Iismanager
{
<summary>
The main entry point for the application.
</summary>
[STAThread]
static void Main (string[] args)
{
Iismanager test = new Iismanager ();
Test. Connect ();
VirtualDirectory VD = new VirtualDirectory ();
Vd. Path = @ "c:\Inetpub\wwwroot\ project folder name";
Vd. Name = "Bkdeip/webui/mobile";

if (test. Exists (VD. Name))
//{
Test. Delete (VD. Name);
//}
Else
//{
Test. Create (VD);
//}
Test. Create (VD);
}

Define what needs to be used.
private string _server,_website;
Private Virtualdirectories _virdirs;
protected System.DirectoryServices.DirectoryEntry RootFolder;
private bool _batchflag;
Public Iismanager ()
{
Use localhost by default, that is, access to the local machine
_server = "localhost";
_website = "1";
_batchflag = false;
}
Public Iismanager (String strserver)
{
_server = strserver;
_website = "1";
_batchflag = false;
}
<summary>
Defining Public properties
</summary>

The server property defines the name of the access machine, which can be IP and the computed name
public string Server
{
get{return _server;}
set{_server = value;}
}
Website attribute definition, for a number, for convenience, use string
In general, the first host is 1, the second host is 2, and so on
public string WebSite
{
get{return _website;}
set{_website = value;}
}

The name of the virtual directory
Public Virtualdirectories Virdirs
{
get{return _virdirs;}
set{_virdirs = value;}
}
<summary>
Defining public methods
</summary>

Connecting to a server
public void Connect ()
{
Connecttoserver ();
}
For convenience overload
public void Connect (string strserver)
{
_server = strserver;
Connecttoserver ();
}
For convenience overload
public void Connect (String strserver,string strwebsite)
{
_server = strserver;
_website = Strwebsite;
Connecttoserver ();
}
Determine whether to save this virtual directory
public bool Exists (string strvirdir)
{
Return _virdirs. Contains (Strvirdir);
}
Add a virtual directory
public void Create (VirtualDirectory newdir)
{
String strpath = "iis://" + _server + "/w3svc/" + _website + "/root/" + newdir. Name;
if (!_virdirs. Contains (Newdir. Name) | | _batchflag)
{
Try
{
Add to the children collection of root
DirectoryEntry Newvirdir = RootFolder. Children.add (Newdir. Name, "IIsWebVirtualDir");
Newvirdir.invoke ("AppCreate", true);
Newvirdir.commitchanges ();
Rootfolder.commitchanges ();
And then update the data
Updatedirinfo (Newvirdir,newdir);
}
catch (Exception ee)
{
throw new Exception (EE. ToString ());
}
}
Else
{
throw new Exception ("This virtual directory is already exist.");
}
}
Get a virtual directory
Public VirtualDirectory Getvirdir (string strvirdir)
{
VirtualDirectory tmp = NULL;
if (_virdirs. Contains (Strvirdir))
{
TMP = _virdirs. Find (Strvirdir);
((VirtualDirectory) _virdirs[strvirdir]). flag = 2;
}
Else
{
throw new Exception ("This virtual directory isn't exists");
}
return TMP;
}

Update a virtual directory
public void Update (VirtualDirectory dir)
{
Determine if the virtual directory you want to change exists
if (_virdirs. Contains (dir. Name))
{
DirectoryEntry ode = RootFolder. Children.find (dir. Name, "IIsWebVirtualDir");
Updatedirinfo (Ode,dir);
}
Else
{
throw new Exception ("This virtual directory isn't exists.");
}
}
 
Delete a virtual directory
public void Delete (string strvirdir)
{
if (_virdirs. Contains (Strvirdir))
{
Object[] paras = new object[2];
Paras[0] = "IIsWebVirtualDir"; Represents the virtual directory of the operation
PARAS[1] = Strvirdir;
RootFolder. Invoke ("Delete", paras);
Rootfolder.commitchanges ();
}
Else
{
throw new Exception ("Can" t Delete "+ Strvirdir +", because it isn ' t exists. ");
}
}
Batch Update
public void UpdateBatch ()
{
BatchUpdate (_virdirs);
}
Overload a:-)
public void UpdateBatch (Virtualdirectories VDS)
{
BatchUpdate (VDS);
}
 
<summary>
Private method
</summary>

Connecting to a server
private void Connecttoserver ()
{
String strpath = "iis://" + _server + "/w3svc/" + _website + "/root";
Try
{
This.rootfolder = new DirectoryEntry (strpath);
_virdirs = Getvirdirs (This.rootfolder.Children);
}
catch (Exception e)
{
throw new Exception ("Can" T connect to the server ["+ _server +"] ... ", e);
}
}
Perform bulk update
private void BatchUpdate (Virtualdirectories VDS)
{
_batchflag = true;
foreach (Object item in VDS. Values)
{
VirtualDirectory VD = (virtualdirectory) item;
Switch (Vd.flag)
{
Case 0:
Break
Case 1:
Create (VD);
Break
Case 2:
Update (VD);
Break
}
}
_batchflag = false;
}
Update Dongdong
private void Updatedirinfo (DirectoryEntry de,virtualdirectory VD)
{
De. properties["AnonymousUserName"][0] = VD. AnonymousUserName;
De. properties["AnonymousUserPass"][0] = VD. AnonymousUserPass;
De. properties["AccessRead"][0] = VD. AccessRead;
De. properties["AccessExecute"][0] = VD. AccessExecute;
De. properties["AccessWrite"][0] = VD. AccessWrite;
De. properties["AuthBasic"][0] = VD. AuthBasic;
De. properties["AUTHNTLM"][0] = VD. AUTHNTLM;
De. properties["ContentIndexed"][0] = VD. contentindexed;
De. properties["EnableDefaultDoc"][0] = VD. EnableDefaultDoc;
De. properties["EnableDirBrowsing"][0] = VD. enabledirbrowsing;
De. properties["AccessSSL"][0] = VD. AccessSSL;
De. properties["AccessScript"][0] = VD. AccessScript;
De. properties["DefaultDoc"][0] = VD. DefaultDoc;
De. properties["Path"][0] = VD. Path;
De.commitchanges ();
}

Get the virtual directory collection
Private Virtualdirectories Getvirdirs (Directoryentries des)
{
Virtualdirectories tmpdirs = new Virtualdirectories ();
foreach (DirectoryEntry de in Des)
{
if (DE. schemaClassName = = "IIsWebVirtualDir")
{
VirtualDirectory VD = new VirtualDirectory ();
Vd. Name = de. Name;
Vd. AccessRead = (bool) de. properties["AccessRead"][0];
Vd. AccessExecute = (bool) de. properties["AccessExecute"][0];
Vd. AccessWrite = (bool) de. properties["AccessWrite"][0];
Vd. AnonymousUserName = (string) de. properties["AnonymousUserName"][0];
Vd. AnonymousUserPass = (string) de. properties["AnonymousUserName"][0];
Vd. AuthBasic = (bool) de. properties["AuthBasic"][0];
Vd. AUTHNTLM = (bool) de. properties["AUTHNTLM"][0];
Vd. ContentIndexed = (bool) de. properties["ContentIndexed"][0];
Vd. EnableDefaultDoc = (bool) de. properties["EnableDefaultDoc"][0];
Vd. EnableDirBrowsing = (bool) de. properties["enabledirbrowsing"][0];
Vd. AccessSSL = (bool) de. properties["AccessSSL"][0];
Vd. AccessScript = (bool) de. properties["AccessScript"][0];
Vd. Path = (string) de. properties["Path"][0];
Vd.flag = 0;
Vd. DefaultDoc = (string) de. properties["DefaultDoc"][0];
Tmpdirs. ADD (VD. NAME,VD);
}
}
return tmpdirs;
}

}
///<summary>
///virtualdirectory class
///</summary>
public class VirtualDirectory
{
private bool _read,_execute,_script,_ssl,_write,_authbasic,_authntlm,_indexed,_endirbrow,_endefaultdoc;
private String _ausername,_auserpass,_name,_path;
Private int _flag;
private String _defaultdoc;
///<summary>
///constructor
///</summary>
Public virtualdirectory ()
{
SetValue ()
}
Public VirtualDirectory (string strvirdirname)
{
_name = strvirdirname;
SetValue ();
}
private void SetValue ()
{
_read = True;_execute = False;_script = false;_ssl= false;_write=false;_ Authbasic=false;_authntlm=false;
_indexed = False;_endirbrow=false;_endefaultdoc = false;
_flag = 1;
_defaultdoc = "default.htm,default.aspx,default.asp,index.htm";
_path = "c:\\";
_ausername = ""; _auserpass = ""; _name= "";
}
///<summary>
///define properties, IIsVirtualDir too many properties
///I only have a few more important, the other guys need to add it.
///</summary>

public int Flag
{
get{return _flag;}
set{_flag = value;}
}
public bool AccessRead
{
get{return _read;}
set{_read = value;}
}
public bool AccessWrite
{
get{return _write;}
set{_write = value;}
}
public bool AccessExecute
{
get{return _execute;}
set{_execute = value;}
}
public bool AccessSSL
{
get{return _SSL;}
set{_ssl = value;}
}
public bool AccessScript
{
get{return _script;}
set{_script = value;}
}
public bool AuthBasic
{
get{return _authbasic;}
set{_authbasic = value;}
}
public bool AUTHNTLM
{
get{return _AUTHNTLM;}
set{_AUTHNTLM = value;}
}
public bool ContentIndexed
{
get{return _indexed;}
set{_indexed = value;}
}
public bool EnableDirBrowsing
{
get{return _endirbrow;}
set{_endirbrow = value;}
}
public bool EnableDefaultDoc
{
get{return _endefaultdoc;}
set{_endefaultdoc = value;}
}
public string Name
{
get{return _name;}
set{_name = value;}
}
public string Path
{
get{return _path;}
set{_path = value;}
}
public string DefaultDoc
{
get{return _defaultdoc;}
set{_defaultdoc = value;}
}
public string AnonymousUserName
{
get{return _ausername;}
set{_ausername = value;}
}
public string AnonymousUserPass
{
get{return _auserpass;}
set{_auserpass = value;}
}
}
<summary>
Collection Virtualdirectories
</summary>

public class VirtualDirectories:System.Collections.Hashtable
{
Public Virtualdirectories ()
{
}
Add a new method
Public VirtualDirectory Find (string strName)
{
Return (VirtualDirectory) this[strname];
}
}
}

Thanks for reading!



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.