Wil Peck

Experience is something you don't get until just after you need it.


News


We automate monitoring of our applications 24/7/365.  However, there are certain days of the year we don’t want to get notified depending on the type of monitoring we are doing.  Especially when we are monitoring for stale dates on various processes when our customers in the US aren’t doing business.  Below are a couple of simple functions that get a list of US holidays that you can use in your applications.  I used a couple references.

http://en.wikipedia.org/wiki/Public_holidays_in_the_United_States

http://www.archives.gov/federal-register/codification/executive-order/11582.html

private static HashSet<DateTime> GetHolidays(int year)
        {
            HashSet<DateTime> holidays = new HashSet<DateTime>();
            //NEW YEARS
            DateTime newYearsDate = AdjustForWeekendHoliday(new DateTime(year, 1, 1).Date);
            holidays.Add(newYearsDate);
            //MEMORIAL DAY  -- last monday in May
            DateTime memorialDay = new DateTime(year, 5, 31);
            DayOfWeek dayOfWeek = memorialDay.DayOfWeek;
            while (dayOfWeek != DayOfWeek.Monday)
            {
                memorialDay = memorialDay.AddDays(-1);
                dayOfWeek = memorialDay.DayOfWeek;
            }
            holidays.Add(memorialDay.Date);
           
            //INDEPENCENCE DAY
            DateTime independenceDay = AdjustForWeekendHoliday(new DateTime(year, 7, 4).Date);
            holidays.Add(independenceDay);
        
            //LABOR DAY -- 1st Monday in September
            DateTime laborDay = new DateTime(year, 9, 1);
            dayOfWeek = laborDay.DayOfWeek;
            while(dayOfWeek != DayOfWeek.Monday)
            {
                laborDay = laborDay.AddDays(1);
                dayOfWeek = laborDay.DayOfWeek;
            }
            holidays.Add(laborDay.Date);

            //THANKSGIVING DAY - 4th Thursday in November
            var thanksgiving = (from day in Enumerable.Range(1, 30)
                           where new DateTime(year, 11, day).DayOfWeek == DayOfWeek.Thursday
                           select day).ElementAt(3);
            DateTime thanksgivingDay = new DateTime(year, 11, thanksgiving);
            holidays.Add(thanksgivingDay.Date);

            DateTime christmasDay = AdjustForWeekendHoliday(new DateTime(year, 12, 25).Date);
            holidays.Add(christmasDay);
            return holidays;
        }

        public static DateTime AdjustForWeekendHoliday(DateTime holiday)
        {
            if (holiday.DayOfWeek == DayOfWeek.Saturday)
            {
                return holiday.AddDays(-1);
            }
            else if (holiday.DayOfWeek == DayOfWeek.Sunday)
            {
                return holiday.AddDays(1);
            }
            else
            {
                return holiday;
            }
        }



Came across this today when I switched from WIX 3.0 and VS 2008 to WIX 3.5 and VS 2010.  The solution ended up being pretty simple.  Just need to update the Wix Project Properties to provide an additional parameter to the compiler and linker. These can be found at Wix Installer Project Properties > Tool Settings > Additional Parameters Compiler and Wix Installer Project Properties > Tool Settings > Additional Parameters Linker.  Just make sure to add ‘-ext WixIIsExtension’ in the fields and recompile.

 

Technorati Tags: ,,


More than once I have come across the issue where we have had a problem using an X509Cert from the certificate store.  Everything is configured properly in the certificate store but when we attempt to create the signature we end up with a cryptographic exception for no apparent reason.

See CryptographicException: The handle is invalid post by Benoit Martin explains the problem and shows how this issue can be resolved.

Technorati Tags: ,,


I recently encountered this error on my Vista x64 box and came across a post that provided ended up providing the resolution.

Link to information about MSI script-based custom action error codes 2738 and 2739

On my system I went to the C:\Windows\SysWOW64 directory and re-registered vbscript.dll and jscript.dll.  Once I did this my WIX project built and I no longer received the 4 ICE offenses (ICE08, ICE09, ICE32 and ICE61).

 

Technorati Tags: ,


Ever need to parse SQL Syntax using .NET without actually executing the SQL?  Pretty sweet because you can actually do this with built in sql server options.  Works out pretty well.  Executing a statement with a syntax error, referencing an object that doesn’t exist, a stored procedure with parameters that has no parameters, etc produces a SqlException with an appropriate error message.

   1:  static void ParseQuery(string sql)
   2:  {
   3:      SqlConnection connection = new SqlConnection("some_connection_string");
   4:      SqlCommand command = new SqlCommand(
   5:          string.Format("SET NOEXEC ON {0} SET NOEXEC OFF", sql));
   6:      command.Connection = connection;
   7:      try
   8:      {
   9:          connection.Open();
  10:          command.ExecuteNonQuery();
  11:      }
  12:      finally
  13:      {
  14:          connection.Close();
  15:      }
  16:  }
Technorati Tags: ,,,


Recently I was tasked with the responsibility of purging database from one of our SQL 2000 databases.  One of the test cases is to use a copy of a database that is a small production database and delete all of the data contained within.  After the delete is executed then I can review the data tables and see what is left over or was skipped after the delete script runs.  Looking at the properties of hundreds of tables in Enterprise Manager seemed pretty tedious. 

After a quick query to Bing I was able to find a decent reference where someone had already done the work for me. 

 

http://sqlserver2000.databases.aspfaq.com/how-do-i-get-a-list-of-sql-server-tables-and-their-row-counts.html

Technorati Tags: ,


I recently had the need to read storage files and extract streams from them for a prototype.  Originally I was able to do this executing StgOpenStorage via PINVOKE from .NET which worked great.  However, due to time constraints and legacy code we really needed to implement this in VB6 as an added feature to an existing OCX with tons of other functionality built in. 

I ended up using Desaware’s Storage Tools 2.5 to read the streams from the structured storage files which worked great.  Desaware provides nice sample applications to understand how to use their API into the storage files.  Pretty straight forward implementation that I was able to complete in a very reasonable amount of time.

Technorati Tags: ,,


I recently had the need to set the output path of the log files at runtime in log4net.  For some reason up until now I had never attempted to accomplish this.  As corporate permissions are more and more restrictive I found the need to set the FileAppender.File path at runtime to a location within the users profile. It turns out accomplishing this task is quite simple. 

public static void Initialize(string logDirectory)
{
    //get the current logging repository for this application
    ILoggerRepository repository = LogManager.GetRepository();
    //get all of the appenders for the repository
    IAppender[] appenders = repository.GetAppenders();
    //only change the file path on the 'FileAppenders'
    foreach (IAppender appender in (from iAppender in appenders
                                    where iAppender is FileAppender
                                    select iAppender))
    {
        FileAppender fileAppender = appender as FileAppender;
        //set the path to your logDirectory using the original file name defined
        //in configuration
        fileAppender.File = Path.Combine(logDirectory, Path.GetFileName(fileAppender.File));
        //make sure to call fileAppender.ActivateOptions() to notify the logging
        //sub system that the configuration for this appender has changed.
        fileAppender.ActivateOptions();
    }
}

HTH - Wil

 

Technorati Tags: ,,,


Really no magic here and just a simple add on to my previous post just in case you  need something that can easily copy the contents of a given directory to another location.

 

public static void CopyContentsTo(this DirectoryInfo source, string desinationDirectory)
{
    if (string.IsNullOrEmpty(desinationDirectory))
    {
        throw new ArgumentNullException("rootDestinationDirectory");
    }

    if (!Directory.Exists(desinationDirectory))
    {
        Directory.CreateDirectory(desinationDirectory);
    }

    foreach (string file in Directory.GetFiles(source.FullName))
    {
        File.Copy(file, Path.Combine(desinationDirectory, Path.GetFileName(file)), true);
    }

    foreach (DirectoryInfo directory in source.GetDirectories())
    {
        CopyTo(directory, desinationDirectory, true);
    }
}

 



The .NET Framework does not offer support for a directory copy method OOB.  Fortunately in .NET 3.5 we can decorate the DirectoryInfo method with our own CopyTo method using an extension method.  See the below example…

public static class DirectoryInfoExtensions
{
    public static void CopyTo(this DirectoryInfo source, string rootDestinationDirectory, bool recursive)
    {
        if (string.IsNullOrEmpty(rootDestinationDirectory))
        {
            throw new ArgumentNullException("destinationDirectory");
        }

        if (!Directory.Exists(rootDestinationDirectory))
        {
            Directory.CreateDirectory(rootDestinationDirectory);
        }

        string destinationDirectoryToCreate = Path.Combine(rootDestinationDirectory, source.Name);

        if (!Directory.Exists(destinationDirectoryToCreate))
        {
            Directory.CreateDirectory(destinationDirectoryToCreate);
        }

        foreach (string file in Directory.GetFiles(source.FullName))
        {
            File.Copy(file, Path.Combine(destinationDirectoryToCreate, Path.GetFileName(file)), true);
        }

        if (recursive)
        {
            foreach (DirectoryInfo directory in source.GetDirectories())
            {
                CopyTo(directory, destinationDirectoryToCreate, true);
            }
        }
    }

    public static void CopyTo(this DirectoryInfo source, string destinationDirectory)
    {
        CopyTo(source, destinationDirectory, false);
    }
}