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.

Set default domain for website programitacally using C#

Inorder to set the default domain for a website in case of Basic Authentication:

 

//set the default domain
DirectoryEntry  site = new DirectoryEntry("IIS://localhost/W3SVC/" + newWebSiteID + "/ROOT");
 site.Invoke("Put", new object[] { "DefaultLogonDomain", "CodeLeacher"});
 site.Invoke("SetInfo");
site.CommitChanges();

IIS Enums

Here are all the enum values needed for the IIS6 development:

public enum AccessPermissions

{

    Read = 1,

    Write = 2,

    Execute = 4,

    Source = 16,

    Script = 512,

    NoRemoteRead = 4096,

    NoRemoteWrite = 1024,

    NoRemoteExecute = 8192,

    NoRemoteScript = 16384,

    NoPhysicalDir = 32768

}

public enum SiteStatus

{

    Starting = 1,

    Started = 2,

    Stopping = 3,

    Stopped = 4,

    Pausing = 5,

    Paused = 6,

    Continuing = 7

}

public enum AppPoolStatus

{

    Starting = 1,

    Started = 2,

    Stopping = 3,

    Stopped = 4

}

public enum AuthenticationTypes

{

    Anonymous = 1,

    Basic = 2,

    Integrated = 4,

    Digest = 16,

    Passport = 64

}

public enum AppPoolIdentity

{

    LocalSystem = 0,

    LocalService = 1,

    NetworkService = 2,

    UserDefined = 3

}

public enum ASPNET

{

    v1_1_4322 = 1,

    v2_0_50727 = 2

}

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

            }

        }

Validate regular expressions immediately

I ran across this nice tool that validates input against regular expressions. It is going to save you a lot of time in case you are trying to develop a regular expression.

http://tools.netshiftmedia.com/regexlibrary/#