SoFunction
Updated on 2025-03-07

C# implementation details of creating, deleting, searching, and configuring virtual directory instances

This article describes the method of creating, deleting, finding and configuring virtual directories in C#. Share it for your reference. The details are as follows:

#region<<Virtual Directory>>/// &lt;summary&gt;
/// Create a virtual directory/// &lt;/summary&gt;
/// <param >Virtual Directory Alias</param>/// <param >Path where the content is located</param>public static bool CreateVirtualDirectory(string webSite,string virtualName, string path)
{
 if (() || (!(path)))
 {
  ("1. The virtual directory alias cannot be empty /r/n 2. The path where the virtual directory contents may not exist", "hint", , );
  return false;
 }
 try
 {
  DirectoryEntry tbEntry = FindVirtualDir(webSite,virtualName);//Find virtual directory  if (tbEntry == null) //Create a virtual directory if it cannot be found  {
   string IISpath = "IIS://localhost/W3SVC/"+webSite+"/ROOT";//IIS parameter format    root = new DirectoryEntry(IISpath);
   tbEntry = (virtualName, );//Create a virtual directory  }
  return UpdateVirDir(tbEntry, path, virtualName);
 }
 catch (Exception ex)
 {
  ("1.Please confirmIISIs it already installed/r/n2.The virtual directory may already exist/r/n (Error:" + (), "hint", , );
  WriterLog("CreateVir:/r/n" + ());
  return false;
 }
}
/// &lt;summary&gt;
/// Get the website list/// &lt;/summary&gt;
/// &lt;returns&gt;&lt;/returns&gt;
public static List&lt;webSiteInfo&gt; GetWebSiteList()
{
 try
 {
  List&lt;webSiteInfo&gt; infoList = new List&lt;webSiteInfo&gt;();
  DirectoryEntry deRoot = new DirectoryEntry("IIS://localhost/W3SVC");
  foreach (DirectoryEntry e in )
  {
   webSiteInfo info = new webSiteInfo();
   PropertyValueCollection pvc = ["ServerBindings"];
   if ( &lt;= 0)
    continue;
   String[] srvBindings = pvc[0].ToString().Split(':');
    = srvBindings[0] ==  ? () : srvBindings[0];//Binding IP address    = srvBindings[1];// Corresponding port number    = ;//The site index   (info);
  }
  return infoList;
 }
 catch (Exception ex)
 {
  WriterLog(());
  return null;
 }
}
/// &lt;summary&gt;
/// Delete virtual directory/// &lt;/summary&gt;
/// &lt;param &gt;&lt;/param&gt;
/// &lt;param &gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
public static bool DelVirtualDirectory(string webSite,string nameDirectory)
{
 try
 {
  DirectoryEntry deRoot = new DirectoryEntry("IIS://localhost/W3SVC/" + webSite + "/ROOT");
  try
  {
   (FindVirtualDir(webSite,nameDirectory));
   deRoot = null;
   return true;
  }
  catch
  {
   return true;
  }
 }
 catch (Exception e)
 {
  WriterLog(());
  return false;
 }
}
/// &lt;summary&gt;
/// Modify the virtual directory/// &lt;/summary&gt;
/// &lt;param &gt;&lt;/param&gt;
/// &lt;param &gt;&lt;/param&gt;
/// &lt;param &gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
public static bool UpdateVirDir(DirectoryEntry tbEntry, string path, string virtualName)
{
 if (tbEntry == null)
  return false;
 ["Path"][0] = path;//Physical Directory ("AppCreate", true);//Create an application ["AccessRead"][0] = true;//Accessible ["ContentIndexed"][0] = true; ;//
 ["DefaultDoc"][0] = ",";
 ["AppFriendlyName"][0] = virtualName;//Application name ["AccessScript"][0] = true;//Access permissions ["DontLog"][0] = true;//log ["AuthFlags"][0] = 1;//Verification, 0 no, 1 allow, 3 basic identity ();//Start apply properties return true;
}
/// &lt;summary&gt;
/// Find virtual directory/// &lt;/summary&gt;
/// &lt;param &gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
public static DirectoryEntry FindVirtualDir(string webSite,string virtualName)
{
 if (())
 {
  ("The virtual directory alias cannot be empty", "hint", , );
  return null;
 }
 try
 {
  string IISpath = "IIS://localhost/W3SVC/" + webSite + "/ROOT";//IIS parameter format   root = new DirectoryEntry(IISpath);//Instance IIS Class  return (virtualName, );//Find a virtual directory) }
 catch (Exception ex)
 {
  WriterLog("The virtual directory does not exist: " + );
  return null;
 }
}
/// &lt;summary&gt;
/// Add an extension to the virtual directory/// &lt;/summary&gt;
/// <param >Virtual Directory Name</param>/// <param >Extension List</param>public static void SetMime(string webSite,string virtualName, Dictionary&lt;string, string&gt; exList)
{
 if (())
 {
  ("The virtual directory alias cannot be empty", "hint", , );
  return;
 }
 DirectoryEntry tbEntry = FindVirtualDir(webSite, virtualName);
 if (tbEntry == null)
  return;
  mime = ["MimeMap"];
 foreach (string e in )
 {
   Ex = new ();
   = e;
   = exList[e];
  if (ContianValue(Ex, mime) != null)
   continue;
  (Ex);
  Ex = null;
 }
 ();
}
/// &lt;summary&gt;
/// Delete the extension/// &lt;/summary&gt;
/// &lt;param &gt;&lt;/param&gt;
/// &lt;param &gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
public static bool DelMime( type,string webSite, string virtualName)
{
 DirectoryEntry tbEntry = FindVirtualDir(webSite, virtualName);
 if (tbEntry == null)
  return false;
  mime = ["MimeMap"];
 object v = ContianValue(type, mime);
 if (v != null)
  (v);
 ();
 return true;
}
/// &lt;summary&gt;
/// Search for the specified extension object/// &lt;/summary&gt;
/// &lt;param &gt;&lt;/param&gt;
/// &lt;param &gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
private static object ContianValue( type, PropertyValueCollection mime)
{
 foreach (object v in mime)
 {
   e = ()v;
  if (().Equals(()) &amp;&amp; (().Equals(())))
  {
   e = null;
   return v;
  }
 }
 return null;
}
/// &lt;summary&gt;
/// Get a list of extensions for virtual directories/// &lt;/summary&gt;
/// &lt;param &gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
public static Dictionary&lt;string, string&gt; GetMimeList(string webSite,string virtualName)
{
 if (())
 {
  ("The virtual directory alias cannot be empty", "hint", , );
  return null;
 }
 try
 {
  Dictionary&lt;string, string&gt; Res = new Dictionary&lt;string, string&gt;();
  DirectoryEntry tbEntry = FindVirtualDir(webSite,virtualName);
  if (tbEntry == null)
   return null;
   _mime = ["MimeMap"];
  foreach (object e in _mime)
  {
    v = ()e;
   if (!())
    (, );
   v = null;
  }
  return Res;
 }
 catch (Exception ex)
 {
  WriterLog("GetMimeList; " + );
  return null;
 }
}
#endregion

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