SoFunction
Updated on 2025-03-01

How to operate the iis root directory in c#

This article describes the method of operating the iis root directory in C#. Share it for your reference. The specific implementation method is as follows:

using System; 
using ; 
using ; 
namespace IISManagement
{ 
/// <summary> 
/// A summary description of IISManager./// </summary> 
public class IISManager 
{ 
//Define what needs to be usedprivate string _server,_website; 
private VirtualDirectories _virdirs; 
protected  rootfolder; 
private bool _batchflag; 
public IISManager() 
{ 
// By default, localhost is used, that is, accessing the local machine_server = "localhost"; 
_website = "1"; 
_batchflag = false; 
} 
public IISManager(string strServer) 
{ 
_server = strServer; 
_website = "1"; 
_batchflag = false; 
} 
/// <summary> 
/// Define public properties/// </summary> 
//The Server attribute defines the name of the access machine, which can be IP and computed namepublic string Server 
{ 
get{ return _server;} 
set{ _server = value;} 
} 
//WebSite attribute is defined as a number, for convenience, use string// Generally speaking, the first host is 1, the second host is 2, and so onpublic string WebSite 
{ 
get{ return _website; } 
set{ _website = value; } 
} 
//The name of the virtual directorypublic VirtualDirectories VirDirs 
{ 
get{ return _virdirs; } 
set{ _virdirs = value;} 
} 
///<summary> 
///Define public methods///</summary> 
//Connect the serverpublic void Connect() 
{ 
ConnectToServer(); 
} 
//For convenience of reloadingpublic void Connect(string strServer) 
{ 
_server = strServer; 
ConnectToServer(); 
} 
//For convenience of reloadingpublic void Connect(string strServer,string strWebSite) 
{ 
_server = strServer; 
_website = strWebSite; 
ConnectToServer(); 
} 
//Judge whether this virtual directory is storedpublic bool Exists(string strVirdir) 
{ 
return _virdirs.Contains(strVirdir); 
} 
//Add a virtual directorypublic void Create(VirtualDirectory newdir) 
{ 
string strPath = "IIS://" + _server + "/W3SVC/" + _website + "/ROOT/" +
; 
if(!_virdirs.Contains() || _batchflag ) 
{ 
try 
{ 
//Add to the Children collection of ROOTDirectoryEntry newVirDir =
(,"IIsWebVirtualDir"); 
("AppCreate",true); 
(); 
(); 
//Then update the dataUpdateDirInfo(newVirDir,newdir); 
} 
catch(Exception ee) 
{ 
throw new Exception(()); 
} 
} 
else 
{ 
throw new Exception("This virtual directory is already exist.");

} 
} 
//Get a virtual directorypublic 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 is not exists"); 
} 
return tmp; 
} 
//Update a virtual directorypublic void Update(VirtualDirectory dir) 
{ 
//Judge whether the virtual directory to be changed existsif(_virdirs.Contains()) 
{ 
DirectoryEntry ode =
(,"IIsWebVirtualDir"); 
UpdateDirInfo(ode,dir); 
} 
else 
{ 
throw new Exception("This virtual directory is not exists."); 
} 
} 
//Delete a virtual directorypublic void Delete(string strVirdir) 
{ 
if(_virdirs.Contains(strVirdir)) 
{ 
object[] paras = new object[2]; 
paras[0] = "IIsWebVirtualDir"; //It means that the operation is a virtual directoryparas[1] = strVirdir; 
("Delete",paras); 
(); 
} 
else 
{ 
throw new Exception("Can''t delete " + strVirdir + ",because it
isn''t exists."); 
} 
} 
//Batch updatepublic void UpdateBatch() 
{ 
BatchUpdate(_virdirs); 
} 
//Overload one:-)public void UpdateBatch(VirtualDirectories vds) 
{ 
BatchUpdate(vds); 
} 
///<summary> 
///Private method///</summary> 
//Connect the serverprivate void ConnectToServer() 
{ 
string strPath = "IIS://" + _server + "/W3SVC/" + _website +"/ROOT"; 
try 
{
 = new DirectoryEntry(strPath);
_virdirs = GetVirDirs(); 
} 
catch(Exception e) 
{ 
throw new Exception("Can''t connect to the server ["+ _server
+"] ...",e); 
} 
} 
//Perform batch updateprivate void BatchUpdate(VirtualDirectories vds) 
{ 
_batchflag = true; 
foreach(object item in )
{ 
VirtualDirectory vd = (VirtualDirectory)item; 
switch() 
{ 
case 0: 
break; 
case 1: 
Create(vd); 
break; 
case 2: 
Update(vd); 
break; 
} 
} 
_batchflag = false; 
} 
//Update Dongdongprivate void UpdateDirInfo(DirectoryEntry de,VirtualDirectory vd) 
{ 
["AnonymousUserName"][0] = ; 
["AnonymousUserPass"][0] = ; 
["AccessRead"][0] = ; 
["AccessExecute"][0] = ; 
["AccessWrite"][0] = ; 
["AuthBasic"][0] = ; 
["AuthNTLM"][0] = ; 
["ContentIndexed"][0] = ; 
["EnableDefaultDoc"][0] = ; 
["EnableDirBrowsing"][0] = ; 
["AccessSSL"][0] = ; 
["AccessScript"][0] = ; 
["DefaultDoc"][0] = ; 
["Path"][0] = ; 
(); 
} 
//Get virtual directory collectionprivate VirtualDirectories GetVirDirs(DirectoryEntries des) 
{ 
VirtualDirectories tmpdirs = new VirtualDirectories(); 
foreach(DirectoryEntry de in des) 
{ 
if( == "IIsWebVirtualDir") 
{ 
VirtualDirectory vd = new VirtualDirectory(); 
 = ; 
 = (bool)["AccessRead"][0]; 
 =
(bool)["AccessExecute"][0]; 
 = (bool)["AccessWrite"][0];
 =
(string)["AnonymousUserName"][0]; 
 =
(string)["AnonymousUserName"][0]; 
 = (bool)["AuthBasic"][0]; 
 = (bool)["AuthNTLM"][0]; 
 =
(bool)["ContentIndexed"][0];
 =
(bool)["EnableDefaultDoc"][0]; 
 =
(bool)["EnableDirBrowsing"][0]; 
 = (bool)["AccessSSL"][0]; 
 =
(bool)["AccessScript"][0]; 
 = (string)["Path"][0]; 
 = 0;
 = (string)["DefaultDoc"][0];
(,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 = ",,,"; 
_path = "C:\\"; 
_ausername = "";_auserpass ="";_name=""; 
} 
///<summary> 
///Define attributes, IISVirtualDir has too many attributes///I have only done more important things, and everyone needs to add them yourself.///</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 :  
{ 
public VirtualDirectories() 
{ 
}
//Add new methodpublic VirtualDirectory Find(string strName) 
{ 
return (VirtualDirectory)this[strName]; 
} 
} 
}

I hope this article will be helpful to everyone's C# programming.