Tuesday, May 11, 2010

Windows Vista \ Windows 7 \ Windows Server 2008 folder permissions

My C# Log File or Configuration File throws a System.UnauthorizedAccessException in Vista / Windows 7 / Windows Server 2008!

I am sure most people have encountered an Access Exception in C# .Net after installing an application that was:

  • Written to run on Windows XP

  • Written to run Windows Server 2003

  • Written to run on an even earlier version of Windows...

after installing it on a :

  • Windows Vista computer

  • Windows 7 computer

  • Windows Server 2008 server

System.UnauthorizedAccessException

at LogOMatic.Logger.Append(String message, Priority level, Priority logPriorityLevel)

at SyncInvokeMemConnect(Object , Object[] , Object[] )

at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)

at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)

at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)

at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)

at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)


Back in the good old days of windows application development files could be written willy-nilly across the hard drive! They were glorious days, but all good things must come to an end.

Modern windows applications (Windows Vista, Windows 7, Windows Server 2008) are expected to only read and write files to certain predefined folder paths.

http://msdn.microsoft.com/en-us/library/system.environment.specialfolder%28VS.100%29.aspx

In the good old days we used to use the default execution path or the assembly path to reference our configuration or log files like so:

// THE OLD XP WAY
String assmLoc = Assembly.GetExecutingAssembly().Location;

Now we need to use the Environment.SpecialFolder class.

If you are running a client side application you can use the ApplicationData special folder like the following example:

// New UAC Way
String APPLICATION_NAME = "TEST";

String assmLoc = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)+"\\"+APPLICATION_NAME;


This will put your configuration or log file in the hidden folder:

Windows Vista / Windows 7 / Windows Server 2008:

C:\ProgramData\\Application Data\TEST

Windows XP / Windows Server 2003:

C:\Documents and Settings\\Application Data\TEST

If you are running a windows service or WCF (Windows Communication Foundation) applciation (like a SOAP web service) you will need to use the CommonApplicationData special folder entry.

//New UAC way for WCF or Services
String APPLICATION_NAME = "TEST";

Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)+"\\"+APPLICATION_NAME

This will put your configuration or log files into the Hidden folder C:\ProgramData\ (could be on a different partition depending on your server configuration):

Windows Vista / Windows 7 / Windows Server 2008:

C:\ProgramData\TEST

Windows XP / Windows Server 2003:

C:\Documents and Settings\TEST




About Synergist Software
Synergist Software is a Calgary, Alberta (Canada) based software company focused on Rich Internet Application (RIA) development.
Please visit us at:
http://www.synergist.ca

4 comments:

  1. Android apps trigger more than a billion of phones and tablets around the world. Yet that is customizable, and in order to develop the phone or mobiles with the additional features terra form has the effective mobile phone apps developer and android app development is also introduced.
    Android Apps Development

    ReplyDelete
  2. We are mainly involved into apps development. Custom apps are being built that matches the requirements of the clients.
    App Store Marketing Calgary

    ReplyDelete
  3. Hello my friend! I would like to tell you that this write-up is awesome, great written and include almost all important info. I would like to see a lot more articles like this.
    windows mobile apps development

    ReplyDelete
  4. I’m really amazed with your posting skills as well as with the layout on your blog site. Is this a paid style or did you modify it yourself? Either way keep up the pleasant quality writing, it is rare to see a great site such as this one these days.
    custom website development company usa

    ReplyDelete