Tuesday, 13 November 2012

Long File Paths in .NET

This may be something that most people are unaware of but Windows has a nice legacy feature which limits path names to 260 characters - try to create a hierarchy that exceeds this and you will get a nice error message. However with enough persistence you can get round this limitation and create paths greater than the limit. 

The problem with this is when you come to read the paths through .NET code everything falls over. This was the problem I was faced with recently at work whilst trying to move some files from one place to another.

I tried a few different things to get around this - creating a network share to make the path smaller, using the folder short names but nothing worked. I then discovered that Windows does have a mechanism for dealing with this by using this path format  "\\?\". This tells it to use long paths which can then use a maximun of 32,767 characters.

Of course nothing is that easy and the .NET methods simply do not handle this situation so the only real solution is to call into the Windows API directly. I started to have a look round for a library that already did this and voila I came across AlphaFS an open source project which does exactly that.

The library recreates all the methods you might need for interacting with directories/files etc but with the twist that you can now pass in the long path format and it will work correctly!




Sunday, 25 March 2012

Visual Studio 11 - First Impressions

I have just installed the new Visual Studio 2011 Beta for testing (yes I am a bit slow here but some of us have work to do) so here are my initial thoughts after a week playing with it.

Install
I elected to install the Ultimate version, if you intend to do the same be warned you are going to need plenty of patience, it took a good 2 hours and 2 reboots before I was up and running.

Design
This is probably the biggest change and possibly the worst. Microsoft has decided that colour is a bad thing and distracts developers from what matters i.e. writing code. I can certainly see the logic in this – the point of an IDE is to make it quicker and easier to get things done, anything which gets in the way of that is just noise.

Unfortunately, as is customary for MS they have got it spectacularly wrong and what they have ended up with is a boring bland UI which feels like a step backwards (it reminds me more of VS 2005 rather than VS 2010). Having just recently been converted to the world of Apple it is not too hard to see where they have got the inspiration from but it has the distinct feel of being designed by developers rather than by designers.

The default theme which is installed, the “light” theme, is particularly bad so the first thing that you should do is change to the slightly better “dark” theme – anyone who has used Expression Blend will feel right at home with this.

Possibly the worst part of the new UI (which seems to be universally hated) is the new panel separators which shout at you in big capital letters!

VS 2010 Roundtrip
Upgrading to new versions of VS in the past was something of a pain if more than one person was working on the project. Opening an old project in the new version would work and allow you to upgrade the project (most of the time) but in the process would destroy backwards compatibility so unless everyone on the team had upgraded you simply couldn't use the new version.

This is now much easier – you can open a VS 2010 solution in VS11 without breaking the project for other developers. There are a few caveats to this mainly that the project must have been created with VS2010 SP1 and if you make any VS 11 specific changes to the project it will no longer open in 2010.

Speed
In terms of speed nothing much seems to have changed from 2010, in fact the whole thing has the feel of more of an incremental change rather than a major update unlike the difference from 2008 > 2010 where it was completely rewritten.

Preview
If you single click on a file it will open a read-only “preview” of the file as a new tab on the right – start editing and it will open it up for editing and move to the main tabs on the left. Personally I see no good reason for this and can be just confusing.

Multi Monitor Support
VS 2010 introduced better support for multiple monitors and this has been enhanced in 11 – I have not fully tested this but it seems to do the job fairly well.

This is as far as I have got at the moment. Putting the design to one side I actually think this is a very impressive update, lets just hope Microsoft have a bit of a change of heart by the time the full version is released and add some colour.

My next challenge is to build a Metro app....

Thursday, 1 March 2012

My Gear

Well after a number of years my home office is starting to take shape so thought I would do a quick blog about what gear I am using and what I hope to purchase soon.



iMac 27-inch: 2.7GHz upgraded to 8GB of RAM


Got this as a bit of present to myself last year and it is a beast, I have been without a desktop for a while choosing to use my laptop for work purposes but decided to invest knowing it would get some usage.

On it I have installed CS5 - mainly because I had some Flash work to get done but also for Illustrator which my wife uses. I have also installed VMware fusion which runs Windows 7 so that I can run Visual Studio and is my main working environment.

iPad 2 64GB 3G


Also got this last year as payment in kind for some work I did (sorry Mr Taxman) - if I am honest 64GB is far to much for me to fill and have not even bothered purchasing a sim for the 3G but its all good. I use it much more than I thought I would so big thumbs up but then there is the iPad 3..hmmm.

iPhone 4S 64GB


I had an HTC Desire for a long time, which I rate as one of the best phones I have ever had, but with the iPad and iMac all set up using iCloud the logical step when it came to upgrading was to go for the newest iPhone. Again if I am honest going for the 64GB one was probably unnecessary but it is nice anyway.

Dell XPS M1530


I bought this laptop about 3 years ago now and spent well over a grand on it upgrading various components and it is still going strong (despite having to replace the battery) - its size means it is not exactly portable and with the iPad doesn't see as much action as it used to but still worth keeping.

HP Photosmart C4585


A good solid all-in-one wireless printer - does what it says on the tin.

The Future


For the future I would probably like a more lightweight netbook/laptop (or whatever the term is these days) for taking on the move plus I am currently considering getting a second monitor for my iMac - I would like to buy one of the Apple Thunderbolt displays but for the sake of my wallet will probably look for a cheaper alternative.

Beyond that I am on the hunt for a good picture to go above my desk (my fabulous wife intends on painting me something herself at some point) and a new mousemat.




Thursday, 16 February 2012

Abstracting the Configuration Manager

Whilst browsing a few blogs earlier this week I came across this post which talks about creating a wrapper around the ASP.NET ConfigurationManager.  For those who don't know the ConfigurationManager class makes pulling settings out of the Web.Config (or App.Config) very straightforward.

I found this post interesting for two reasons -
  1. It encapsulates (at least for me) the difference between a fresh out of University programmer and a more experienced programmer. 
  2. I don't think it goes far enough - it can be extended further by introducing an abstraction with the added benefit of making it much easier to unit test.
As an inexperienced programmer just starting out it is all to easy (and I have done it myself) to start littering code with lines like this -

string emailTo = ConfigurationManager.AppSettings["emailTo"];

There are a few things wrong with doing this but the most important is what happens if for some reason it is decided to remove the application settings from the config file and use a database table instead? The only real option open to you in this case is an error prone find and replace routine - not ideal!

The answer is to not directly reference the ConfigurationManager but as the article suggests create a wrapper around it - so you would create an AppConfig class with methods for getting settings (see the referenced post for implementation details).

If this class is used correctly and consistently throughout the application then you now have only one class in your application where ConfigurationManager is referenced so now if a change is required to store in a database you only have to update the code in a single class.

This is where the post finishes and a year or so ago I probably would have as well but there is still a problem with it - what if after changing the code to read from a database your (indecisive) boss decides he wants to change back? You can start to see why this is not a particularly good way to go about things - it breaks the Open/Closed principal - once a class is built it should not be modified (except in the case of bugs which is a source of debate).

There is a much more elegant way to do this - simply create an abstraction. Couple this with Dependancy Injection and it becomes a very flexible solution to the problem. The way I would do it is to create the following interface -

public interface IConfigurationManager
{
   public string GetSetting(string key, string defaultValue);
   //Other methods for different types
}

Then you can refactor the AppConfig class to implement the interface - you can then refactor your calling code to use the interface rather than a concrete class and use Dependency Injection to supply an implementation at runtime.

Now if you need to change to a database storage system all you need to do is create a new class that implements the IConfigurationManager interface such as -

public class DatabaseSettings : IConfigurationManager
{
    public string GetSetting(string key, string defaultValue)
    {
        //pull setting from DB
    }
}

Now the only place where you will need to alter any code is the code that wires up the Dependancy Injection configurations (which could be none at all if you have configured through XML/config file) - now the Open/Closed principal is not being violated and swapping back to the old method is simply a matter of changing the dependency injection configuration! A far better way to do things I'm sure you will agree.

Of course there is still more than can be done with this although I think that is good enough for now but for a more comprehensive example see this post

I'm Back

This blogging thing is trickier than I thought - having the time to actually sit down and right a blog post is one thing but actually having something to say is another entirely! 

After several months of silence I am going to have another try - this time I aiming to do at least 1 blog post every week - so lets see how it goes.