asp.net implementation of IIS control management---Web virtual directory creation and management

Source: Internet
Author: User
Tags foreach define bool contains iis connect
asp.net|iis|web| Create | control | virtual directory

A CreateWebDir.cs Use Example

string sserver = "localhost";
String virtualdir = "Bug2004"; \ virtual Directory
String pathdir = @ "D:\myWebDirTest\Bug2000" \ \ Physical Directory

Iismanager iismg = new Iismanager (sserver);
Iismg.connect ();

if (Iismg.exists (Virtualdir]))
{
Console.Write (virtualdir+ "is exist!");
}
Else
{
Myini.iniwritevalue ("WebDir", "Webdirexist", "false");

Iismg.get_anonymoususer ();
string[] Anonymoususer = new string[2];
Anonymoususer[0] = Iismg.anonymoususername;
ANONYMOUSUSER[1] = Iismg.anonymoususerpass;

VirtualDirectory Newvirdir = new VirtualDirectory (virtualdir,pathdir,anonymoususer);
if (Iismg.create (Newvirdir))
Console.Write (virtualdir+ "Create success!");
Else
Console.Write (virtualdir+ "Create unsuccessful!");
}

Iismg.close ();

Two CreateWebDir.cs, I made some improvements and revisions.

///***********************************************************
///**************                 IIS control management class                  **************
///************** pastes from : Flying knives   http://www.aspcn.com; *************
///***********************************************************
using System;
using System.Data;
using System.DirectoryServices;
using System.Collections;

Namespace Createwebdir
{
<summary>
Summary description of the Iismanager.
</summary>
public class Iismanager
{
Define what needs to be used.
private string _server,_website,_anonymoususerpass,_anonymoususername;
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>

public void Get_anonymoususer ()
{
_anonymoususerpass= "Iusr_deve-server";
_anonymoususername= "Iusr_deve-server";
VirtualDirectory VDir;
Try
{
Hashtable myList = (Hashtable) _virdirs;
IDictionaryEnumerator myenumerator = Mylist.getenumerator ();
while (Myenumerator.movenext ())
{
VDir = (virtualdirectory) myenumerator.value;
if (vdir.anonymoususername!= "" && vdir.anonymoususerpass!= "")
{
_anonymoususername=vdir.anonymoususername;
_anonymoususerpass=vdir.anonymoususerpass;
Break
}
}
}
Catch
{
_anonymoususerpass= "Iusr_deve-server";
_anonymoususername= "Iusr_deve-server";
}
}
public string AnonymousUserName
{
get{return _anonymoususername;}
set{_anonymoususername = value;}
}
public string AnonymousUserPass
{
get{return _anonymoususerpass;}
set{_anonymoususerpass = value;}
}
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 bool 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);
return true;
}
catch (Exception ee)
{
throw new Exception (EE. ToString ());
return false;
}
}
Else
{
return true;
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>

public void Close ()
{
_virdirs. Clear ();
_virdirs = null;
RootFolder. Dispose ();

}
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>
Constructors
</summary>
Public VirtualDirectory ()
{
Setvalue ();
}
Public VirtualDirectory (String svirdirname)
{
Setvalue ();
_name = Svirdirname;
}
Svirdirname: virtual path;
Strphypath: Physical path (physics path)
Public VirtualDirectory (String svirdirname,string strphypath,string[] anonymoususer)
{
Setvalue ();
_name = Svirdirname;
_path = Strphypath;
_ausername = anonymoususer[0];
_auserpass = anonymoususer[1];
}
private void Setvalue ()
{
_read = True;_execute = False;_script = True;_ssl= false;_write=false;_authbasic=false;
_authntlm=true;_indexed = True;_endirbrow=false;_endefaultdoc = true;
_flag = 1;
_defaultdoc = "default.htm,default.aspx,default.asp,index.htm";
_path = "c:\\";
_ausername = "Iusr_deve-server"; _auserpass = "Iusr_deve-server"; _name= "";
}
<summary>
Define attributes, IIsVirtualDir too many attributes
I've only got something more important, the others 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];
}
}
}



Related Article

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.