<< back to blog

Strongly typed file names in .NET

.net ~ golem

27 Sep, 2008

Working with filename strings in C# is always a pain in the ass. Whilst playing around with Golem, I thought it would be nice to be able to have strongly typed access to the file system for when I’m coding file-system related stuff (copying files, clearing log files etc).

What if you could write something like this:

C.Code.Foo.Foo_sln.CopyTo( C.Code.Bar );

I actually wanted a structure that assist with working with .NET solution files, rather than mimicking the file system.

To get the strong typing, I wrote a really crappy bit of code that scans the file-system for a VS.NET solution and generates code for a strongly typed representation of it. So you can then do things like…

   Solution.BuildProject.Configs.Windsor_Dev.File.CopyTo(Solution.WebProject.Directory );
   Solution.BuildProject.Configs.Windsor_Test.File.CopyTo(Solution.TestProject.Directory );

Intellisense on your solution file structure is rather fun! You can’t get that in XML :)

The code to get this isn’t very clever - it just recurses on the FS and generates more c# code, which is a hierarchy of static classes with FileInfo and DirectoryInfo properties that point to the correct places on the file system.

The generated code looks like this:

public static class Solution
{
   ///<summary> Points to c:\Code\Foo\Foo.sln</summary>
   public static FileInfo File = new FileInfo("c:\\Code\\Foo\Foo.sln");

   ///<summarty>Points to c:\Code\Foo\Foo.Build\Foo.Build.csproj</summary>
   public static class BuildProject  
   {
        public static FileInfo File = new FileInfo(...);
        public static class Configs{ ... }
   }
}

I only spent 30 minutes on it, but I quite like the direction it’s taking. Can you think of any cool features?

Would this be a useful standalone OS project?

You may also like...
.NET Gem Fun
Running FubuMVC on Mono (OSX)
Really Easy Way To Partition an ASP.NET MVC With An Admin Area
Automating .NET development (and NHibernate) with IronRuby + Rake
Get a Rails-Stylee Interactive Shell For Your NHibernate Backed Domain Model
In Memory SQLite Testing with NHibernate
NHibernate Trick
OSX MonoDevelop MVC?
NHibernate: Calling Update Unnecessarily
.NET Config for Multiple Developers

kick it on DotNetKicks.com
blog comments powered by Disqus