C# and Virtual Directory

  • Thread starter Thread starter Jacky Luk
  • Start date Start date
J

Jacky Luk

Hi all,
If I need to make a C# application, do I need to set up a virtual dir such
as inetroot on the Harddisks. How do I set up such thingy?
Thanks
Jack
 
Using the System.DirectoryServices namespace classes.
Herewith something (not tested) to get you started...

Willy.

using System;
using System.DirectoryServices;
using System.IO;
class IISVdir {
// Authorization flags
const int MD_AUTH_ANONYMOUS = 0x00000001; //Anonymous authentication
available.
const int MD_AUTH_BASIC = 0x00000002; //Basic authentication available.
const int MD_AUTH_NT = 0x00000004; //Windows authentication schemes
available.
// Browse flags
const int MD_DIRBROW_SHOW_DATE = 0x00000002; //Show date.
const int MD_DIRBROW_SHOW_TIME = 0x00000004; // Show time.
const int MD_DIRBROW_SHOW_SIZE = 0x00000008; //Show file size.
const int MD_DIRBROW_SHOW_EXTENSION = 0x00000010; //Show file name
extension.
const int MD_DIRBROW_LONG_DATE = 0x00000020; //Show full date.
const int MD_DIRBROW_LOADDEFAULT = 0x40000000; // Load default page, if it
exists.
const uint MD_DIRBROW_ENABLED = 0x80000000;
// Access Flags
const int MD_ACCESS_READ = 0x00000001; //Allow read access.
const int MD_ACCESS_WRITE = 0x00000002; //Allow write access.
const int MD_ACCESS_EXECUTE = 0x00000004; //Allow file execution
(includes script permission).
const int MD_ACCESS_SOURCE = 0x00000010; //Allow source access.
const int MD_ACCESS_SCRIPT = 0x00000200; // Allow script execution.
const int MD_ACCESS_NO_REMOTE_WRITE = 0x00000400; // Local write access
only.
const int MD_ACCESS_NO_REMOTE_READ = 0x00001000; // Local read access
only.
const int MD_ACCESS_NO_REMOTE_EXECUTE = 0x00002000; // Local execution
only.
const int MD_ACCESS_NO_REMOTE_SCRIPT = 0x00004000; // Local host access
only.

public static void Main() {
// Create a virtual directory and application in the IIS Root
CreateVirtualDirectory("IIS://localhost/W3SVC/1/Root", "someWeb",
"c:\\somefolder");
}

public static void CreateVirtualDirectory(string parent, string
virtDirectory, string physicalDir)
{
// Delete VDir if already exists
if(CheckIfExists(parent, virtDirectory))
{
Delete(parent, virtDirectory, physicalDir);
}
// Create physical directory if non existent
if (!Directory.Exists(physicalDir))
{
Directory.CreateDirectory(physicalDir);
}
// remove / or \\ at end of path if any
string str = physicalDir;
if (physicalDir.EndsWith("/") || physicalDir.EndsWith("\\"))
{
str = physicalDir.Remove(physicalDir.Length - 1, 1);
}
DirectoryEntry folderRoot = new DirectoryEntry(parent);
folderRoot.RefreshCache();

DirectoryEntry vDir =
folderRoot.Children.Add(virtDirectory,"IIsWebVirtualDir");
vDir.CommitChanges();
// Set Properties
vDir.Properties["Path"].Value =str;
vDir.Properties["AuthFlags"].Value = MD_AUTH_ANONYMOUS | MD_AUTH_NT;
vDir.Properties["DefaultDoc"].Value = "default.aspx";
vDir.Properties["DirBrowseFlags"].Value = MD_DIRBROW_SHOW_DATE |
MD_DIRBROW_ENABLED |
MD_DIRBROW_SHOW_SIZE | MD_DIRBROW_SHOW_EXTENSION |
MD_DIRBROW_LONG_DATE | MD_DIRBROW_LOADDEFAULT;
vDir.Properties["AccessFlags"].Value = MD_ACCESS_READ |
MD_ACCESS_SCRIPT;
// add a custom header, format is (key: value)
object[] headers = {"TestValue: 1", "AnotherOne: 1234"};
vDir.Properties["HttpCustomHeaders"].AddRange(headers);
// Call AppCreat2 to create a Web application (0 =In-proc, 1 =
Out-proc, 2 = Pooled) , required by IIS, not used by ASP.NET
object[] applicationType = new object[]{0};
vDir.Invoke("AppCreate2", applicationType);
// Save Changes
vDir.CommitChanges();
folderRoot.CommitChanges();
vDir.Close();
folderRoot.Close();
}

internal static bool CheckIfExists(string RootWeb, string VirtualDirectory)
{
DirectoryEntry directoryEntry = new DirectoryEntry(String.Concat(RootWeb,
"/", VirtualDirectory));
try
{
string name = directoryEntry.Name;
return true;
}
catch
{
bool flag = false;
return flag;
}
}

internal static void Delete(string RootWeb, string VirtualDirectory, string
PhysicalDirectory)
{
try
{
DirectoryEntry root = new DirectoryEntry(RootWeb);
DirectoryEntry vdir = root.Children.Find(VirtualDirectory,
root.SchemaClassName);
// Remove Entry from container.
string strName = vdir.Name;
root.Children.Remove(vdir);
Console.WriteLine(strName+ " entry is removed from container.");
root.CommitChanges();
root.Close();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
// delete physical dir
Directory.Delete(PhysicalDirectory, true);
}
}
 
Thanks and it's really nice info :). I had another question, when I
opened/created a C# web service or web application.. It didn't allow me to
do so, it just prompt me to open a virtual directory. In the case, do I
artifically create inetroot on the C Drive?
Thanks in advance
Jack

Willy Denoyette said:
Using the System.DirectoryServices namespace classes.
Herewith something (not tested) to get you started...

Willy.

using System;
using System.DirectoryServices;
using System.IO;
class IISVdir {
// Authorization flags
const int MD_AUTH_ANONYMOUS = 0x00000001; //Anonymous authentication
available.
const int MD_AUTH_BASIC = 0x00000002; //Basic authentication available.
const int MD_AUTH_NT = 0x00000004; //Windows authentication schemes
available.
// Browse flags
const int MD_DIRBROW_SHOW_DATE = 0x00000002; //Show date.
const int MD_DIRBROW_SHOW_TIME = 0x00000004; // Show time.
const int MD_DIRBROW_SHOW_SIZE = 0x00000008; //Show file size.
const int MD_DIRBROW_SHOW_EXTENSION = 0x00000010; //Show file name
extension.
const int MD_DIRBROW_LONG_DATE = 0x00000020; //Show full date.
const int MD_DIRBROW_LOADDEFAULT = 0x40000000; // Load default page, if it
exists.
const uint MD_DIRBROW_ENABLED = 0x80000000;
// Access Flags
const int MD_ACCESS_READ = 0x00000001; //Allow read access.
const int MD_ACCESS_WRITE = 0x00000002; //Allow write access.
const int MD_ACCESS_EXECUTE = 0x00000004; //Allow file execution
(includes script permission).
const int MD_ACCESS_SOURCE = 0x00000010; //Allow source access.
const int MD_ACCESS_SCRIPT = 0x00000200; // Allow script execution.
const int MD_ACCESS_NO_REMOTE_WRITE = 0x00000400; // Local write access
only.
const int MD_ACCESS_NO_REMOTE_READ = 0x00001000; // Local read access
only.
const int MD_ACCESS_NO_REMOTE_EXECUTE = 0x00002000; // Local execution
only.
const int MD_ACCESS_NO_REMOTE_SCRIPT = 0x00004000; // Local host access
only.

public static void Main() {
// Create a virtual directory and application in the IIS Root
CreateVirtualDirectory("IIS://localhost/W3SVC/1/Root", "someWeb",
"c:\\somefolder");
}

public static void CreateVirtualDirectory(string parent, string
virtDirectory, string physicalDir)
{
// Delete VDir if already exists
if(CheckIfExists(parent, virtDirectory))
{
Delete(parent, virtDirectory, physicalDir);
}
// Create physical directory if non existent
if (!Directory.Exists(physicalDir))
{
Directory.CreateDirectory(physicalDir);
}
// remove / or \\ at end of path if any
string str = physicalDir;
if (physicalDir.EndsWith("/") || physicalDir.EndsWith("\\"))
{
str = physicalDir.Remove(physicalDir.Length - 1, 1);
}
DirectoryEntry folderRoot = new DirectoryEntry(parent);
folderRoot.RefreshCache();

DirectoryEntry vDir =
folderRoot.Children.Add(virtDirectory,"IIsWebVirtualDir");
vDir.CommitChanges();
// Set Properties
vDir.Properties["Path"].Value =str;
vDir.Properties["AuthFlags"].Value = MD_AUTH_ANONYMOUS | MD_AUTH_NT;
vDir.Properties["DefaultDoc"].Value = "default.aspx";
vDir.Properties["DirBrowseFlags"].Value = MD_DIRBROW_SHOW_DATE |
MD_DIRBROW_ENABLED |
MD_DIRBROW_SHOW_SIZE | MD_DIRBROW_SHOW_EXTENSION |
MD_DIRBROW_LONG_DATE | MD_DIRBROW_LOADDEFAULT;
vDir.Properties["AccessFlags"].Value = MD_ACCESS_READ |
MD_ACCESS_SCRIPT;
// add a custom header, format is (key: value)
object[] headers = {"TestValue: 1", "AnotherOne: 1234"};
vDir.Properties["HttpCustomHeaders"].AddRange(headers);
// Call AppCreat2 to create a Web application (0 =In-proc, 1 =
Out-proc, 2 = Pooled) , required by IIS, not used by ASP.NET
object[] applicationType = new object[]{0};
vDir.Invoke("AppCreate2", applicationType);
// Save Changes
vDir.CommitChanges();
folderRoot.CommitChanges();
vDir.Close();
folderRoot.Close();
}

internal static bool CheckIfExists(string RootWeb, string VirtualDirectory)
{
DirectoryEntry directoryEntry = new DirectoryEntry(String.Concat(RootWeb,
"/", VirtualDirectory));
try
{
string name = directoryEntry.Name;
return true;
}
catch
{
bool flag = false;
return flag;
}
}

internal static void Delete(string RootWeb, string VirtualDirectory, string
PhysicalDirectory)
{
try
{
DirectoryEntry root = new DirectoryEntry(RootWeb);
DirectoryEntry vdir = root.Children.Find(VirtualDirectory,
root.SchemaClassName);
// Remove Entry from container.
string strName = vdir.Name;
root.Children.Remove(vdir);
Console.WriteLine(strName+ " entry is removed from container.");
root.CommitChanges();
root.Close();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
// delete physical dir
Directory.Delete(PhysicalDirectory, true);
}
}




Jacky Luk said:
Hi all,
If I need to make a C# application, do I need to set up a virtual dir such
as inetroot on the Harddisks. How do I set up such thingy?
Thanks
Jack
 
Back
Top