Get or set the filesystem folder (path) for a IIS website, with C#

        /// <summary>
        /// Get the filesystem folder for the given website
        /// </summary>
        /// <param name="serverName">Name of the IIS server e.g. localhost</param>
        /// <param name="websiteName">Name of the website e.g. test</param>
        /// <returns>filesystem folder or empty if not found</returns>
        public string GetWebSiteFileSystemFolder(string serverName, string websiteName)
        {
            if (string.IsNullOrEmpty(serverName)) { throw new Exception("Parameter [serverName] can't be null or empty"); }
            if (string.IsNullOrEmpty(websiteName)) { throw new Exception("Parameter [websiteName] can't be null or empty"); }
            string result = string.Empty;

            DirectoryEntry w3svc = new DirectoryEntry(string.Format("IIS://{0}/w3svc", serverName));

            foreach (DirectoryEntry site in w3svc.Children)
            {
                if (site.Properties["ServerComment"] != null)
                {
                    if (site.Properties["ServerComment"].Value != null)
                    {
                        if (string.Compare(site.Properties["ServerComment"].Value.ToString(), websiteName, false) == 0)
                        {

                            foreach (DirectoryEntry child in site.Children)
                            {
                                if (child.Properties["Path"] != null)
                                {
                                    if (child.Properties["Path"].Value != null)
                                    {
                                        result = child.Properties["Path"].Value.ToString();
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return result;
        }
 

Change the Propery “Path” to “ServerBindings” to change serverbindings like “:80:testwebsite”

Get IIS website id on website name, with C#

        /// <summary>
        /// Get website id on websitename
        /// </summary>
        /// <param name="serverName">Name of the IIS server e.g. localhost</param>
        /// <param name="websiteName">Name of the website e.g. test</param>
        /// <returns>
        /// Less the 0, site does not exist
        /// Id of the existing site
        /// </returns>
        public int GetWebSiteId(string serverName, string websiteName)
        {
            int result = -1;

            DirectoryEntry w3svc = new DirectoryEntry(string.Format("IIS://{0}/w3svc", serverName));

            foreach (DirectoryEntry site in w3svc.Children)
            {
                if (site.Properties["ServerComment"] != null)
                {
                    if (site.Properties["ServerComment"].Value != null)
                    {
                        if (string.Compare(site.Properties["ServerComment"].Value.ToString(), websiteName, 
                         false) == 0)
                        {
                            result = site.Name;
                            break;
                        }
                    }
                }
            }

            return result;
        }

Check if an IIS website exists with C#

        /// <summary>
        /// Check if a website on the given server exist.
        /// Check on websitename (ServerComment) is case insensitive.
        /// </summary>
        /// <param name="serverName">Name of the IIS server e.g. localhost</param>
        /// <param name="websiteName">Name of the website e.g. test</param>
        /// <returns></returns>
        public bool DoesWebsiteExist(string serverName, string websiteName)
        {
            bool result = false;

            DirectoryEntry w3svc = new DirectoryEntry(string.Format("IIS://{0}/w3svc", serverName));

            foreach (DirectoryEntry site in w3svc.Children)
            {
                if (site.Properties["ServerComment"] != null)
                {
                    if (site.Properties["ServerComment"].Value != null)
                    {
                        if (string.Compare(site.Properties["ServerComment"].Value.ToString(), websiteName, false) == 0)
                        {
                            result = true;
                        }
                    }
                }
            }

            return result;
        }

NANT: Build Microsoft VisualStudio solutions and configure environments

I use NANT to build Microsoft VisualStudio solutions and after deploying software with msi packages, I use NANT to configure the environment (Creating WebSite, Virtual Directories, Custom Eventlogs, register services, create folders, create shares etc)

More information on NANT can be found, here: http://nantcontrib.sourceforge.net/, including documentation for task, types and functions.

I use Edirot4NANT as an GUI tool to run NANT tasks:

 http://www.download.com/Editor4NAnt/3000-2352_4-10531916.html

Using quotes in XML

To use quotes in xml, use “&quot;

<exec program="CMD.EXE"
              commandline="/C &quot;${installPath}\DatabaseInstallation.cmd&quot;"
              workingdir="${installPath}">
            <arg value="${Database_DataSource}" />

GetFiles, sorted by Creation DateTime, filtered by include file mask and exclude file mask, including subfolders in C#

The “GetFiles” function, returns all files in the given folder, including subfolders, that match the “includeMask”. The result is sorted by the file CreationTime. The function excludes all files that match the “excludeMask”

 

To call the function:

List<FileInfo> files = FileSystemHelper.GetFiles(@"C:\Windows","*.*", "*.exe", SortOrder.Ascending, SearchOption.AllDirectories);

 

The function:
/// <summary>
///
Get files in a folder sorted by creation datetime and filtered by include mask and exclude mask
/// </summary>
/// <param name=”folder”>
The folder containing the files</param>
/// <param name=”includeMask”>
Include files that meet the given mask</param>
/// <param name=”excludeMask”>
Exclude files that meet the given mask</param>
/// <param name=”sortOrder”>
Sort order, ascending or descending</param>
/// <param name=”searchOption”>
AllDirectories or TopDirectoryOnly</param>
/// <returns></returns>
public staticList<FileInfo> GetFiles(string folder, string includeMask, string excludeMask, SortOrder sortOrder, SearchOption searchOption)
{
    DirectoryInfo folderDirectoryInfo = newDirectoryInfo(folder);
    List<FileInfo> files = newList<FileInfo>(folderDirectoryInfo.GetFiles(includeMask, searchOption));
   
    stringexcludeMaskRegEx = FileSystemHelper.WildcardToRegex(excludeMask);

    // exclude files
  
files = (fromfile infiles
             where!Regex.IsMatch(file.Name, excludeMaskRegEx)
             selectfile).ToList<FileInfo>();

    if(files.Count > 0)
    {
        switch(sortOrder)
        {
            caseSortOrder.Ascending:
                // Sort files oldest first
              
files.Sort(
                    delegate(FileInfo firstFile, FileInfo secondFile)
                    {
                        returnfirstFile.CreationTime.CompareTo(secondFile.CreationTime);
                    }
                );
                break;
            caseSortOrder.Descending:
                // Sort files newest first
              
files.Sort(
                    delegate(FileInfo firstFile, FileInfo secondFile)
                    {
                        returnsecondFile.CreationTime.CompareTo(firstFile.CreationTime);
                    }
                );
                break;
        }

    }

    returnfiles;
}

 

/// <summary>
/// Convert a wildcard mask to regular expression
/// </summary>
/// <param name="pattern">wildcard string</param>
/// <returns>Regular expression string</returns>
public static string WildcardToRegex(string pattern)
{
    return string.Format("^{0}$", Regex.Escape(pattern).Replace("\*", ".*").Replace("\?", "."));
}

public enum SortOrder
{
        Ascending,
        Descending
}

SSIS package configuration

We have chosen to use a windows environment variable to hold the path to the *.dtsConfig, which is used to configure the *.dtsx package.

– Close all open Microsoft Visual Studio 2005 instances (because changing windows environment variables will have nog effect)

– Add a Windows Environment Variable:

– start > Control Panel > System > Advanced system settings > Environment Variables > New…

– Variable name: SSISPackage_Configuration

– Variable value: C:\Temp\SSISPackage.dtsConfig

– Click on [OK]

– Click on [OK]

 

– Open the Microsoft Visual Studio 2005 solution containing the SSIS project.

– Open the *.dtsx

– Click in the top menu on SSIS > Package Configurations …

– Check the Enable package configurations

image

– Click on Add …

image

image

image

When you start debugging the SSIS package in Microsoft Visual Studio 2005, the configuration will be red from the C:\Temp\SSISPackage.dtsConfig.

In production you can change the path of the environment variable and the configuration will then be red from that location.