473,379 Members | 1,278 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

C# and Virtual Directory

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
Nov 15 '05 #1
2 14387
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,"IIsWebVirtu alDir");
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" <no****@nospam.com> wrote in message
news:uY**************@TK2MSFTNGP11.phx.gbl...
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

Nov 15 '05 #2
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 [MVP]" <wi*************@pandora.be> ¼¶¼g©ó¶l¥ó·s»D
:uV*************@tk2msftngp13.phx.gbl...
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,"IIsWebVirtu alDir");
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" <no****@nospam.com> wrote in message
news:uY**************@TK2MSFTNGP11.phx.gbl...
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


Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Wm. Scott Miller | last post by:
We have a intranet site that allows one of our departments to search a set of pdfs and then look at them. Only problem is that only they and us geeks should be allowed to see the pdfs. We have it...
7
by: Jaydeep | last post by:
Hi, Anybody knows how to create virtual directory programmatically under root directory ofcourse from code-behind. I am developing web-based application where I need to create a folder and making...
2
by: Danny Miller | last post by:
Hi there, I'm facing a problem that is driving me nuts. The scenario is as follows: 1) Create an empty directory structure e.g. C:\Dev1\TestWebApp 2) Map a virtual directory e.g. TestWebApp...
4
by: david | last post by:
I basically use the following code to display the directory and file names in the WWWROOT, but can not show the virtual directory. ---- Dim path As String = Server.MapPath(x) Dim di As...
6
by: Saar Carmi | last post by:
Hi How can I get the application's virtual directory from the Application_Start method in Global.asax ? Keep in mind there is no request/response object available yet in this stage. Thanks...
1
by: Dave | last post by:
I am getting te following error in a ASP.Net app that is running on Win XP Pro (SP2): Server cannot access application directory 'C:\Documents and Settings\dave\My Documents\My Visual Studio...
8
by: Nate | last post by:
I am running on Window 2003. I have a website built in ASP.NET 2.0. I need to have a Virtual Directory running an application in 1.1. I have configured each in its own Application Pool. The 1.1...
2
by: Rose winsle | last post by:
Hi guys ... I just wanted to create virtual directory using VB.net . i can manage that ... when i create the virtual directory how can i set anonymous access off. following is my code...
6
by: ManagedCoder | last post by:
Hi, My requirement is as follows: I need to set the HttpExpires (enable content expiration - set to 7 days) on a folder within a virtual directory. I have been able to set the HttpExpires...
6
by: Scott M. | last post by:
I didn't get a resolution to this in my earlier post, so I'll try again: System: Windows XP Pro. (SP2) with IIS installed and running PRIOR to VS 2008 Pro. installation. VS 2008 Pro. (full...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.