Creating FTP site on IIS6 programitacally using C#

Inorder to create FTP site on IIS6

        public void CreateFTPSite(string serverName, string siteName, string siteID)

        {

            try

            {   

                //Create website on IIS

                DirectoryEntry root = new DirectoryEntry(“IIS://” + serverName + “/msftpsvc”);

                DirectoryEntry newSite = root.Children.Add(siteID, “IIsFTPServer”);

                newSite.Properties[“ServerComment”][0] = “codeleacher.com”;

                newSite.CommitChanges();

                root.CommitChanges();

 

                SetSingleProperty(“IIS://” + serverName + “/msftpsvc/”+siteID.ToString(), “ServerBindings”,    “:2122:”);

 

        private static void SetSingleProperty(string metabasePath, string propertyName, object newValue)

        {

            try

            {

                DirectoryEntry path = new DirectoryEntry(metabasePath);

                PropertyValueCollection propValues = path.Properties[propertyName];

                if (null == propValues.Value)

                    propValues.Value = “foo”;

 

                string oldType = propValues.Value.GetType().ToString();

                string newType = newValue.GetType().ToString();

                if (newType == oldType)

                {

                    path.Properties[propertyName][0] = newValue;

                    path.CommitChanges();

                }

                else

                    Console.WriteLine(” Failed in SetSingleProperty; type of new value does not match property”);

            }

            catch (Exception ex)

            {

                if (“HRESULT 0x80005006” == ex.Message)

                    Console.WriteLine(” Property {0} does not exist at {1}”, propertyName, metabasePath);

                else

                    Console.WriteLine(“Failed in SetSingleProperty with the following exception: \n{0}”, ex.Message);

            }

        }

            }

            catch (Exception ex)

            {

                throw new Exception(“Error”);

            }

        }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: