Tuesday, August 19, 2008

Handy script to launch a solution file from the command line

Before Resharper came along and clutter my directories with files, I used tab-completion to launch my solution files from the command line using:

START MyApplication.sln

commandlineBut, with two other resharper files now preceeding the solution file I needed another way to launch the solutution file. Here’s the script that I came up with:


@ECHO OFF

FOR %%n IN (*.sln) DO START %%n

If you have more than one solution file in the same directory, this script is not for you, but that is not a likely scenario. I saved this file as sln.bat and put in the \windows\system32 directory, although any directory in your PATH will do.

Monday, August 18, 2008

Parsing Flat files and new Data 2.0 - Part I

This is the first of a two part article on parsing a fixed length line delimited set of records into a C# Object. This month’s MSDN Magazine featured two articles that caught my eye. The first, was the Toolbox written by Scott Mitchell where he makes reference of an open source project filehelpers.com by Marcos Meli. I was excited to see this as a few of our projects need this type of support for manipulating text based data and this seemed to have all of the features that I wanted. The second article I will take a look at the new Data 2.0 features that were released with Visual Studio 2008 SP1 and .NET 3.5 SP1 (see Scott Hanselman’s posting).

Here’s the brief little demo that I want to build as a console application in .NET 3.5. Read a flat file containing prescribers. This happens to be defined by SureScripts and is a format that we use with our eScript Messenger product.

sample-1

There are 42 fields in the 4.0 format of this file. I fired up the FileHelpers Wizard and proceeded to type in the details on the 42 records. The wizard takes you through a series of four dialogs that ask you details on the record structure you will read (fixed length or delimited), the name of the class and the visibility of that class as well as a few more pages on how many fields each of your records will contain. There is support for ignoring the first N or last N lines, ignoring empty lines as well as identifying a comment marker. For the more advanced usages you can even selectively filter each of the records bases on conditions that you provide.

sample-2

sample-2aAbove is a sample that I entered with a preview of the class that the wizard creates for you. Take a look at the button in the lower left. That is a really nifty button that allows you to test your definitions before you copy/save the code files out to your project! Marcos really deserves a lot of credit for a well thought out application.

Before I gush with too many more accolades for FileHelpers, I should point out a couple of issues that I ran into while putting this together. While you can see the optional field checkboxes checked in the dialog above, I discovered that you can only check the Optional Field check box on fields if all of the following fields also have it checked as well. I was presented with an error dialog, but it would have been nicer to see that in the field designer. Hey its free/donation ware.

sample-2b

After I went back and removed all of the optional fields all was well and I was presented with a nice Grid View showing all of the data that I had in my sample data file. Now that is very nice as it reminds me of the old days when I imported flat files into Microsoft Access database and such.

sample-3

To finish this off I saved the generated class as as well as the handy template for loading the sample data, fired up Visual Studio 2008 and created a new console application. Below is the source to read in all of the records:

static void Main()
{

using (var engine = new FileHelperAsyncEngine<Prescriber>())

{

engine.BeginReadFile(@"..\..\sample.txt");

while (engine.ReadNext() != null)

{

Prescriber record = engine.LastRecord;

Console.WriteLine("{0}, {1}", record.LastName, record.FirstName);

}

}

}


sample-3aThat’s really all there was to it. I changed the original template a little to use .NET 3.5 var’s which I am liking more after my initial impressions of them. Incidently, I blame Resharper for that as it likes to provide too many of those helpful hints about changing the variable to an explicit type definition.

So there you have it. I had never used FileHelpers before and had a working prototype up and running in under 20 minutes. I saved the definition file for the flat file so I can go back and make changes to it in the future if necessary.

Here’s the console running in all of its glory. What would we do without our old friends Harry Winston and Anthony Cardino!


sample-4

In Part II I will show how to use the new .NET 3.5 (or 3.6 as Hanselman sez) and stich up some simple data access using LINQ and the new SP1 features.

Tuesday, December 11, 2007

.NET 3.5, NAnt and CruiseControl.NET

My build server at work is a VM that does not have VS 2005/8 installed on it. I like to keep it that way because it keeps the large install footprint from being replicated to the build machine. All of the installs that we do at my company can be build from NAnt command line or we can build inside of VS.

.NET 3.5 and VS 2008 specifically, changed how the tools required to build using NAnt are deployed. Here are the steps I went through in order to get our solutions that were .NET 2.0/VS2005 buildable using NAnt 0.85 w/ NAntContrib running under .NET 3.5:


  1. Download the .NET 3.5 Runtime available here
  2. Download the NAnt 0.86 Beta 1 here
  3. Download the NAntContrib 0.85 Nightly here
  4. If you now attempt to run a nant build file you might find you get an error like



  5. There are several key components that are no longed installed into the RuntimeDirectory (aka C:\Windows\Microsoft.NET\Framework\v3.5. Things like the gacutil, sn.exe, xsd.exe, svcutil.exe etc… are missing.) These can now be found at C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin, but only if you install some extra things from the VS 2008 install CD.

  6. On my CD obtained during the padnug VS2008 Install Party (or alternatively in the ISO you can download from MS). Open the \WCU\WinSDK folder and grab the WinSDK_*.exe files. Copy these up to your build server, and then extract the bits using WinZip (or your favorite ZIP archive program). I noted that you could not run the EXEs directly, note there might be a better way to do this via the VS 2008 Installer CD, but I was following some instructions and a hunch based on Blog de Jérémie Clabaut. (Incidently, I google’d ‘visual studio 2008 SDK gacutil’ to find this)

  7. Once you’ve run the .msi’s that you extracted, you should find gacutil and friends in their new location at C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin. I believe that you only need to install the WinSDK_W32Tools.exe and WinSDK_NetFxTools.exe, but I installed all five of them.

Currently, my projects are still using .NET 2.0 and not .NET 3.5. Most likely there will be some gotchas as most of our stack uses components like NHibernate 1.2, log4.net, etc… that I’ve not yet tested to make sure they work with .NET 3.5. VS 2008 seems to run a lot faster! I’ve not installed any of my old favorite AddIns yet though like, Copy As HTML or Resharper.


One other notable thing: the Cassini Web Server that runs your HTTP applications without deploying to IIS has also changed locations and is located at: C:\Program Files\Common Files\Microsoft Shared\DevServer\9.0\WebDev.WebServer.EXE.

Thursday, September 6, 2007

VMware Workstation, Dell D620 Laptop, and Intel SpeedStep... oh boy!

It turns out that SpeedStep on a laptop is a really bad thing when you are trying to use a VM. The symptoms are that the clock in the VM runs about twice as fast. Note that my laptop report a minimum processor speed of ~1GHz and max at 2GHz. So I noticed that the real clock was record seconds go by between 1.5 - 2x faster than normal! This might explain why a coworked and I could not get our VMs to function correctly on a demo visit to a partner's office. To fix it I rebooted, turned off SpeedStep in the BIOS.

When I don't have a VM on my laptop, i.e. after vacation, I will restore the SpeedStep.

Wednesday, August 2, 2006

Thomas Friedman on Syria, the US and War

If you’ve not heard of Thomas Friedman you should know who he is. He’s a journalist that has published a number of must reads: The World is Flat and Longitudes and Attitudes as well as a really interesting television story on global warming and electric cars: Addicted to Oil (its on again on August 14th)

But anyways… check out the Fresh Air interview from August 1 with Terry Gross. Most notable and disturbing was this quote from Thomas Friedman, “(Hezbollah) hates Israel more than they love their own kids… that is such a travesty.”

There’s more interesting things he has to say about the tragedy of the Accidental War as the Economist calls it.

Friday, April 28, 2006

Quote from Captain James Cook

"I can be bold to say, that no man will ever venture farther than I have done and that the lands which may lie to the South will never be explored."


Captain James Cook, 27 January 1775

I put this quote here as food for thought. At the time Cook wrote this he had good enough reason to make such a strong statement. For well over 2000 years, or perhaps even all of recorded human history to that time, boots were only made of wood. Note, that at this time, there were no steam powered vessels, nor were there the steel and iron ships which would come to be about 100 years later.

When we try to solve problems in our lives we often put too little thought into questioning the base assumptions that we make decisions on.

This also leads into that often quoted addage about those who do not know the past are doomed to repeat it.

Tuesday, January 31, 2006

NANT files and XML namespaces

NANT files and XML namespaces

Being a reader (worshiper?) of Scott's blog I happened upon
Intellisense and NAnt .build files in VS.NET back a while ago. Recently I also starting using nant include tasks to centralize common functions/scripts/targets in a single location. When you combine the two, beware: all included files in which you reference targets need to also include the same namespace for instance:

xmlns="http://nant.sf.net/schemas/nant-0.84.win32.net-1.0.xsd"

The only error you see is:

BUILD FAILED

Target 'foobar' does not exist in this project. It is used from target 'wombat'.


Enjoy!