Adding FTP to WebSite using C#

Adding FTP to WebSite using C#

am 29.10.2007 12:40:44 von Materialised

Hi All,

I am attempting to add ftp support to a website I create. To create the
website, I use the following function:

public class WebSites {
public const string FTPSERVICE = "/MSFTPSVC";
public const string IIS = "IIS://";
public const string WEBSERVICE = "/W3SVC";

public static bool AddSite(WebSites NewWebSite) {
string strIISPath = IIS + NewWebSite.ServerName + WEBSERVICE;

string strPhysicalPath = "\\\\" + NewWebSite.ServerName +
"\\hosts\\" + NewWebSite.SiteName + "\\httpdocs";
// Try to create the Directory
Directory.CreateDirectory("\\\\" + NewWebSite.ServerName +
"\\hosts\\" + NewWebSite.SiteName);


// Create Restricted Directories
Directory.CreateDirectory("\\\\" + NewWebSite.ServerName +
"\\hosts\\" + NewWebSite.SiteName +
"\\httpdocs");
Directory.CreateDirectory("\\\\" + NewWebSite.ServerName +
"\\hosts\\" + NewWebSite.SiteName +
"\\httpdocs\\App_Code");

Directory.CreateDirectory("\\\\" + NewWebSite.ServerName +
"\\hosts\\" + NewWebSite.SiteName +
"\\httpdocs\\App_Data");
Directory.CreateDirectory("\\\\" + NewWebSite.ServerName +
"\\hosts\\" + NewWebSite.SiteName +
"\\httpdocs\\bin");
Directory.CreateDirectory("\\\\" + NewWebSite.ServerName +
"\\hosts\\" + NewWebSite.SiteName +
"\\private");

// Set the Websites Physical Path
NewWebSite.PhysicalPath = "\\\\" + NewWebSite.ServerName +
"\\hosts\\" + NewWebSite.SiteName +
"\\httpdocs";
// Create the New Website
ServerManager iisManager =
ServerManager.OpenRemote(NewWebSite.ServerName);
iisManager.Sites.Add(NewWebSite.SiteName, "http",
NewWebSite.HostHeader, strPhysicalPath);
iisManager.Sites.Add(NewWebSite.SiteName, "ftp",
NewWebSite.HostHeader, strPhysicalPath);
iisManager.CommitChanges();



// Use Active Directory to set the website permissions
DirectoryEntry objIISDirectoryEntry = new
DirectoryEntry(strIISPath + "/" + GetSiteID(strIISPath,
NewWebSite.SiteName));
// Assign property to Website
objIISDirectoryEntry.Properties["EnableDirBrowsing"][0] =
NewWebSite.HasBrowseAccess;
objIISDirectoryEntry.Properties["AccessExecute"][0] =
NewWebSite.HasExecuteAccess;
objIISDirectoryEntry.Properties["AccessRead"][0] =
NewWebSite.HasReadAccess;
objIISDirectoryEntry.Properties["AccessWrite"][0] =
NewWebSite.HasWriteAccess;
objIISDirectoryEntry.Properties["AuthAnonymous"][0] =
NewWebSite.IsAnonymousAccessAllow;
objIISDirectoryEntry.Properties["AuthBasic"][0] =
NewWebSite.IsBasicAuthenticationSet;
objIISDirectoryEntry.Properties["AuthNTLM"][0] =
NewWebSite.IsNTLMAuthenticationSet;

// Save Changes
objIISDirectoryEntry.CommitChanges();

// Close both directory object.
objIISDirectoryEntry.Close();
return true;
}

It uses a Mixture of Active Directory and System.Web.Administration
classes to automate the process.
How can I also create a default website with the same document root as
the site I have just created?
Kind regards
Mick Walker
--
Mick Walker
Software Engineer

http://www.mick-walker.co.uk

Re: Adding FTP to WebSite using C#

am 29.10.2007 19:20:31 von Kristofer Gafvert

What do you mean by "default website"? Where does FTP come in (as implied
by the subject line)?

Or do you maybe mean you want to create a FTP site? Use the CreateNewSite
method.

http://msdn2.microsoft.com/en-us/library/ms524671.aspx

Then set the Path property of the ROOT virtual directory (IIsFtpVirtualDir)

http://msdn2.microsoft.com/en-us/library/ms524560.aspx

Or do you want one website, but several virtual directories (one for each
user)?

Perhaps you want to set this up using IIS Manager, and then examine the
metabase to see what has changed? You can do that with Metabase Explorer.

--
Regards,
Kristofer Gafvert
http://www.gafvert.info/iis/ - IIS Related Info


Mick Walker wrote:

>Hi All,
>
>I am attempting to add ftp support to a website I create. To create the
>website, I use the following function:
>
>public class WebSites {
> public const string FTPSERVICE = "/MSFTPSVC";
> public const string IIS = "IIS://";
> public const string WEBSERVICE = "/W3SVC";
>
> public static bool AddSite(WebSites NewWebSite) {
> string strIISPath = IIS + NewWebSite.ServerName + WEBSERVICE;
>
> string strPhysicalPath = "\\\\" + NewWebSite.ServerName + "\\hosts\\" + NewWebSite.SiteName + "\\httpdocs";
> // Try to create the Directory
> Directory.CreateDirectory("\\\\" + NewWebSite.ServerName + "\\hosts\\" + NewWebSite.SiteName);
>
>
> // Create Restricted Directories
> Directory.CreateDirectory("\\\\" + NewWebSite.ServerName + "\\hosts\\" + NewWebSite.SiteName +
> "\\httpdocs");
> Directory.CreateDirectory("\\\\" + NewWebSite.ServerName + "\\hosts\\" + NewWebSite.SiteName +
> "\\httpdocs\\App_Code");
>
> Directory.CreateDirectory("\\\\" + NewWebSite.ServerName + "\\hosts\\" + NewWebSite.SiteName +
> "\\httpdocs\\App_Data");
> Directory.CreateDirectory("\\\\" + NewWebSite.ServerName + "\\hosts\\" + NewWebSite.SiteName +
> "\\httpdocs\\bin");
> Directory.CreateDirectory("\\\\" + NewWebSite.ServerName + "\\hosts\\" + NewWebSite.SiteName +
> "\\private");
>
> // Set the Websites Physical Path
> NewWebSite.PhysicalPath = "\\\\" + NewWebSite.ServerName + "\\hosts\\" + NewWebSite.SiteName +
> "\\httpdocs";
> // Create the New Website
> ServerManager iisManager = ServerManager.OpenRemote(NewWebSite.ServerName);
> iisManager.Sites.Add(NewWebSite.SiteName, "http", NewWebSite.HostHeader, strPhysicalPath);
> iisManager.Sites.Add(NewWebSite.SiteName, "ftp", NewWebSite.HostHeader, strPhysicalPath);
> iisManager.CommitChanges();
>
>
>
> // Use Active Directory to set the website permissions
> DirectoryEntry objIISDirectoryEntry = new DirectoryEntry(strIISPath + "/" + GetSiteID(strIISPath, NewWebSite.SiteName));
> // Assign property to Website
> objIISDirectoryEntry.Properties["EnableDirBrowsing"][0] = NewWebSite.HasBrowseAccess;
> objIISDirectoryEntry.Properties["AccessExecute"][0] = NewWebSite.HasExecuteAccess;
> objIISDirectoryEntry.Properties["AccessRead"][0] = NewWebSite.HasReadAccess;
> objIISDirectoryEntry.Properties["AccessWrite"][0] = NewWebSite.HasWriteAccess;
> objIISDirectoryEntry.Properties["AuthAnonymous"][0] = NewWebSite.IsAnonymousAccessAllow;
> objIISDirectoryEntry.Properties["AuthBasic"][0] = NewWebSite.IsBasicAuthenticationSet;
> objIISDirectoryEntry.Properties["AuthNTLM"][0] = NewWebSite.IsNTLMAuthenticationSet;
>
> // Save Changes
> objIISDirectoryEntry.CommitChanges();
>
> // Close both directory object.
> objIISDirectoryEntry.Close();
> return true;
> }
>
>It uses a Mixture of Active Directory and System.Web.Administration
>classes to automate the process.
>How can I also create a default website with the same document root as the
>site I have just created?
>Kind regards
>Mick Walker