Posts in category .net - page 10

Using LINQ to foreach over an enum in C#

I can’t be the only person in the world who wants to foreach over the values of an enum otherwise Enum.GetValues(Type enumType) wouldn’t exist in the framework. Alas it didn’t get any generics love in .NET 2.0 and unhelpfully returns an array.

Thanks to the power of LINQ you can do this:

foreach(var customerType in Enum.GetValues(typeof(CustomerTypes)).Cast<CustomerTypes>()))

That is okay, but this is more concise:

foreach(CustomerTypes customerType in Enums.Get<CustomerTypes>())

The tiny class to achieve that is, of course:

using System.Collections.Generic;
using System.Linq;

public static class Enums {
  public static IEnumerable<T> Get<T>() {
    return System.Enum.GetValues(typeof(T)).Cast<T>();



Windows 2008 Server on my MacBook Pro

A troublesome disk (a story for another time) has forced me to reinstall my MacBook Pro and review my Windows partition.

My Boot Camp partition was running Vista Ultimate x86 which felt sluggish, ignored the last 1GB and bugged me with UAC. One Windows update kept failing to install which also prevented SP1 from completing.

Apple’s Boot Camp doesn’t support 64-bit Windows (except on the Mac Pro) and my 64-bit experiences have been unpleasant so far (no Flash for IE x64, limited 64-bit shell extensions, Live! refusing to install, drivers etc.) The increased x64 memory consumption would also be an issue when running in a 1.5GB virtual machine via Parallels or VMware Fusion.

Windows XP was one option but losing IIS7 and DirectX 10 would see me reinstalling Vista within weeks so I decided to try Windows 2008 Server x86.

Boot Camp happily accepted the 2008 Server x86 CD where I chose the BOOTCAMP partition, formatting it as NTFS and electing for a standard installation. The Boot Camp drivers subsequently installed without complaint, all 4GB of RAM was accessible and there are no 64-bit compatibility issues.

Microsoft are giving away 1 year evaluation copies of Windows 2008 Enterprise Server x86 as part of their Heroes Happen Here launch program for Windows 2008, SQL Server 2008 and Visual Studio 2008 if you don’t happen to have an MSDN subscription to hand. There are however a few tweaks you need to do to get a more desktop-like experience:

Install desktop features

Head into Server Manager and Add Features then choose Desktop Experience to install Windows Media Player, Aero etc.

Go into Services and set the Themes service to Automatic and Start it to make themes available and then choose Browse… from the Theme Settings in Personalisation to select %windir%\Resources\Themes\Aero.theme

Install wireless networking

This one had me stumped for a while as I thought my wireless card/drivers weren’t working. The reality is that 2008 Server has wireless networking removed by default so head into Server Manager > Add Features > Wireless LAN Service to install it.

Enabling hibernate

Open a command prompt and enter:

powercfg.exe /hibernate on

Remove annoying shutdown

Head into the registry to HKEY\_LOCAL\_MACHINE\SOFTWARE\Policies\Microsoft\Windows NT\Reliability and change the ShutdownReasonOn DWORD key to __.

Relaxing local password policy

A controversial change I’m sure but I’d rather choose something complex and unique that will last 90+ days than something memorable every 30. Head into Local Security Policy > Account Policies > Password Policy > Maximum password age and change it to something more reasonable.

Going further

A great guide with screen-shots on additional tweaks for a more workstation-like experience also exists – wish I known about that earlier!

Simone Chiaretta has highlighted the that tap-to-click is absent and there are some Bluetooth issues stemming from missing drivers.


Future of AnkhSVN (Subversion for Visual Studio)

AnkhSVN 2.0 is now out with the majority of these features making the cut!

Now that AnkhSVN 1.0.3 is out with support for Visual Studio 2008 we can discuss our future plans for AnkhSVN.

We have moved over to openCollabNet and welcomed Jeremy Whitlock and Bert Huijben to the team!

Our preliminary road-map for AnkhSVN 2.0 is:

  • Improve user experience
  • Refine windows, options and icons
  • Localize dialogs and messages
  • Support customization of icons & menus
  • Develop interactive log window
  • Add keyboard support (short cuts & tab order)
  • Extend integration
  • Subversion 1.5 branch & merge tracking
  • Subversion property editing
  • Visual Studio 2005/2008 source provider model for better integration
  • Foster developer participation
  • Simplify build environment with MSBuild
  • Reduce code-base with SharpSvn
  • Switch Visual Studio API from automation to source control
  • Provide source-on-demand using Sourceserver

Switching the provider model means we have to drop Visual Studio 2003 support for 2.0 but means we get to use .NET 2.0+ features as well as a much faster and more robust mechanism for extending Visual Studio.

The timetable is quite aggressive and I’m hoping we can get quick regular builds out for people to try.


Web site vs web application in Visual Studio

Rob Conery got me thinking about web site maintenance and I put forward a brief comment on the two distinct types and how Visual Studio handles them which I have expanded upon here.

Web site

Primarily for working with ad-hoc web sites that have programmed elements. Easily identified by customer-specific content present in aspx files.

No solution or project files are required and the pages and source can reside locally (file system, IIS) or remotely (FTP, WebDev/FrontPage extensions) via the File > Open > Web Site… menu option.

Code-behind and classes are typically stored on the web server which compiles them in-memory on demand. Changes can be made to the files without restarting the application and losing sessions.

For Against
Quick edit, test, deploy cycle Syntax errors at run-time
No need to compile or restart app Can’t create an installer
Source always available Source on server useful to hackers

Web application

Web application projects were introduced as an add-on for Visual Studio 2005, later rolled in to VS 2005 SP1 and made a full first-class citizen with Visual Studio 2008.

Like the name implies these are primarily for web applications, those times when you have written a product or solution that happens to have a web interface.

Web application projects exist on your local drive and are treated like any other VS project type and can be added to existing solutions are subject to full compilation, validation and build steps.

Deployment is typically via MSI installers however you can also utilize the addition Web Deployment Projects add-in which allows you to deployment directly to servers which is useful for deploying to test environments.

For Against
Controlled build & deploy process Deployment causes application restart
No class files on web server, dll only Can’t deploy individual classes
Syntax errors at compile time


Sander and I were discussing this article and thought an interesting solution might be to use the Web Application model for local development but to use the Publish option to publish all solution files to an intermediate directory.

Then in the intermediate directory just remove the bin/applicationname.dll file and copy to the target. This should prevent an application restart unless the web.config or global.asax/global.asax.vb files have been modified.