473,322 Members | 1,188 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,322 software developers and data experts.

Creating website via DirectoryServices in C# but will not serve aspxpages (htm ok)

I've seen a similar post about this problem but still cannot find an
automated solution. Below is the code I'm using to create a website
(for IIS 6.0) via C#. once the site is created, it will not serve aspx
pages. If I:

1. go to IIS Manager
2. view the properties for the site in question
3. go to the [Home Directory] tab
4. click [Remove] next to the Application Name textbox in the
Application Settings area.
5. click on [Add] to add the application back in.

The site works as expected.

Can anyone help me out? Feel free to email me at

mustafashabib [at] sbcglobal [dot] net

thanks.
Nov 26 '07 #1
1 1376
On Nov 26, 5:44 pm, MMAS <mustafasha...@gmail.comwrote:
I've seen a similar post about this problem but still cannot find an
automated solution. Below is the code I'm using to create a website
(for IIS 6.0) via C#. once the site is created, it will not serve aspx
pages. If I:

1. go to IIS Manager
2. view the properties for the site in question
3. go to the [Home Directory] tab
4. click [Remove] next to the Application Name textbox in the
Application Settings area.
5. click on [Add] to add the application back in.

The site works as expected.

Can anyone help me out? Feel free to email me at

mustafashabib [at] sbcglobal [dot] net

thanks.
sorry, the code is as follows:
public static int CreateNewWebsite(string website_name, int port,
string path_to_website_root)
{
try
{
DirectoryEntry root = new DirectoryEntry("IIS://
localhost/W3SVC");
// Find unused ID value for new web site

bool found_valid_site_id = false;
int random_site_id = 1;
do
{
bool regenerate_site_id = false;
System.Random random_generator = new Random();
random_site_id = random_generator.Next();

foreach (DirectoryEntry e in root.Children)
{
if (e.SchemaClassName == "IIsWebServer")
{

int current_site_id =
Convert.ToInt32(e.Name);
if (current_site_id == random_site_id)
{
regenerate_site_id = true;
break;
}
}
}

found_valid_site_id = !regenerate_site_id;
} while (!found_valid_site_id);

// Create web site

DirectoryEntry site =
(DirectoryEntry)root.Invoke("Create", "IIsWebServer", random_site_id);

site.Invoke("Put", "ServerComment", website_name +
String.Format(" - ({0})", port));

site.Invoke("Put", "KeyType", "IIsWebServer");

site.Invoke("Put", "ServerBindings", ":" +
port.ToString() + ":");

site.Invoke("Put", "ServerState", 2);

//site.Invoke("Put", "FrontPageWeb", 1);

//site.Invoke("Put", "DefaultDoc",
"Default.aspx,Default.html,Default.html,Index.aspx ,Index.htm,Index.html");

// site.Invoke("Put", "SecureBindings", ":443:");

site.Invoke("Put", "ServerAutoStart", 1);

site.Invoke("Put", "ServerSize", 1);

site.Invoke("SetInfo");

//create app website directory
site.AuthenticationType =
AuthenticationTypes.Anonymous;
// site.Username = username;
// site.Password = password;

// DirectoryEntry website_directory =
site.Children.Add("Root", "IIsWebDirectory");
// website_directory.Properties["Location"][0] = "/
LM/W3SVC/" + random_site_id.ToString() + "/root/aspnet_client";
// website_directory.Properties["AccessFlags"][0] =
"AccessRead";
// website_directory.Properties["DirBrowseFlags"]
[0] = "0";
// website_directory.CommitChanges();
// Create application virtual directory
DirectoryEntry virtual_directory =
site.Children.Add("Root", "IISWebVirtualDir");

virtual_directory.Properties["AppIsolated"][0] =
2;

if (path_to_website_root.EndsWith("\\"))
{
path_to_website_root =
path_to_website_root.Substring(0, path_to_website_root.Length - 1);
}

virtual_directory.Properties["Path"][0] =
path_to_website_root;
virtual_directory.Invoke("AppCreate", true);

virtual_directory.Properties["EnableDirBrowsing"]
[0] = false ;
virtual_directory.Properties["AccessExecute"][0] =
true;
virtual_directory.Properties["AccessRead"][0] =
true;
virtual_directory.Properties["AccessWrite"][0] =
false;
virtual_directory.Properties["AuthAnonymous"][0] =
true;
virtual_directory.Properties["AuthBasic"][0] =
false;
virtual_directory.Properties["AuthNTLM"][0] =
true;
virtual_directory.Properties["AppFriendlyName"][0]
= website_name;
virtual_directory.Properties["AppRoot"][0] = "LM/
W3SVC/" + random_site_id.ToString() +"/Root";
virtual_directory.CommitChanges();

site.CommitChanges();
//
RunApplication(Environment.ExpandEnvironmentVariab les(@"%SystemRoot%
\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis. exe"), "-i");// + "/
W3SVC/" + random_site_id.ToString() + "/Root");
virtual_directory.Close();
site.Close();

RunApplication(Environment.ExpandEnvironmentVariab les(@"%SystemRoot%
\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis. exe"), "-s " + "/
W3SVC/" + random_site_id.ToString() +"/Root");

RunApplication(Environment.ExpandEnvironmentVariab les(@"%SystemRoot%
\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis. exe"), "-c");
return random_site_id;
}
catch (Exception ex)
{
throw new Exception("An error occurred trying to
create a website.", ex);
}
}

private static string RunApplication(string location,
string arguments)
{
System.Diagnostics.ProcessStartInfo psi = new
System.Diagnostics.ProcessStartInfo(location);
psi.Arguments = arguments;
psi.RedirectStandardOutput = true;
psi.WindowStyle =
System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
System.Diagnostics.Process listFiles;
listFiles = System.Diagnostics.Process.Start(psi);
System.IO.StreamReader myOutput =
listFiles.StandardOutput;
listFiles.WaitForExit(2000);
string output = "";
if (listFiles.HasExited)
{
output = myOutput.ReadToEnd();

}

return output;
}
Nov 26 '07 #2

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

Similar topics

12
by: hykim | last post by:
Hello, everyone. according to MSDN, there is any constructor of System.DirectoryServices.SearchResultCollection Class. if I implement DirectorySearcher.FindAll() method by myself, then how can I...
1
by: Corne Grotius | last post by:
Hiya, I'm trying to create a new site on IIS 6.0 using ADSI and C# using the following code: DirectoryEntry W3SVC = new DirectoryEntry("IIS://" + ServerName + "/w3svc", Username, Password,...
1
by: Stephanie Stowe | last post by:
Hi. I am trying to read information out of the IIS metabase (v5.1). Observe the following code: using System; using System.DirectoryServices; using System.Reflection; namespace ADSI1 {...
15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
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...
5
by: Sam777 | last post by:
I was under the impression that creating the app_offline.htm file at the root of the webapp would cause all handles to be closed so that the app could be removed. Unfortunately, this isn't the...
1
by: sudhapadmas | last post by:
Hello netters, I was trying to create a virtual directory in IIS using the following code: System.EnterpriseServices.Internal.IISVirtualRoot vr = new...
10
by: jflash | last post by:
Hello all, I feel dumb having to ask this question in the first place, but I just can not figure it out. I am wanting to set my site up using dynamic urls (I'm assuming that's what they're...
1
by: Mick Walker | last post by:
Hi all, I am wondering is it possible to create a physical Directory such as "C:\Test" using the DirectoryServices namespace. If so, does anyone have any resources on how to do it? I should...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.