C# Extension Methods

Extension Methods is new to C# 3.0 and allows you to attach new methods to .Net data types. I have created a simple example. I have added an extension method to the .Net Int32 type to calculate the square value of an integer.

public static class Program
{
static void Main(string[] args)
{
   Int32 num = 2;
   Console.WriteLine(num.Square());
}
public static Int32 Square(this Int32 n)
{
   return n * n;
}
}

I have added the Square function to the Int32 type by adding the this keyword  before the first parameter in the function which informs the compiler that this method is an extension method of the parameter type.

Note: Both the extension method and its class must be static

In case you have an extension method and an instance method with the same name and signature, the instance method will take precedence over the extension method.

Note: Events, Properties, and operators can not be extended.

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”);

            }

        }