.NET Development :: Accessing Special Folders Location Across Different Windows Versions & Tightened Security Within Windows Environment

I am writing this post as I know quite a few developers only now migrating apps from Windows XP environments, most of whom are spending a lot of time fighting with the new tighter security world that started to come in with Windows Vista.

As most know; the directory structure for user files and temporary documents has changed over the life of Windows (“C:\Documents and Settings\…” is now “C:\Users\…”, Program Files location changes for 64 bit, etc).

In addition to this many of the folders and registry keys, that as a developer, you used to be able to write files and values to are no-longer accessible. In addition to this areas of the event log are also locked down, and writing to it can crash your application if not handled correctly.

So now on Windows Vista, Windows 7 and Windows 8, as a developer you cannot and should not be writing to Program Files, the root of any of your drives, Windows folder, etc, etc. This can also include the traditional temp folder (C:\Windows\Temp or C:\Temp).

The only places you can write to with any certainty are the user specific Temp folder, the ProgramData folder, or the users document store.

If you are having to deal with a legacy app you might have to manually (or programatically) override the security settings granting permissions to write files to the locations you need, though you should do so with caution, as Windows updates and security patches can reverse your changes.

All of these locations vary depending on the system setup and operating system, so what is the best way to handle it?

Well in any .NET App you can easily access any of these folders locations using the Environment namespace (System.Environment) and the SpecialFolder enumeration.

For example: –

Console.WriteLine(“Folder Path: {0}”,

A full list of the special folders can be found at http://msdn.microsoft.com/en-gb/library/system.environment.specialfolder.aspx

This is not a list of those accessible for writing to, it is a complete list.

In addition to these you can also access the traditional list of environment variables using: –


Though for this you need to know what is available as it will cause an exception if you call for a variable that does not exist.

You can get a full list of what is on your current system with: –

var s = System.Environment.GetEnvironmentVariables();
foreach (System.Collections.DictionaryEntry item in s)
Console.WriteLine(“{0} = {1}”, item.Key, item.Value);

But you need to avoid app specific ones, and watch out for some that may have changed names over the years.

You can access the documentation (.NET 4.5) for the System.Environment class at http://msdn.microsoft.com/en-us/library/z8te35sa.aspx or via your Visual Studio help.

With regards to the Event Log, you need to make sure you create your event source during your app install (or have an admin add it into the appropriate event log). This may not be caught during development as most developers run as admin and may even turn off all the UAC protection.

If you don’t then the kind of error you might see in Visual Studio would look something like: –

Security Exception when trying to write to Event Log

Security Exception when trying to write to Event Log.
“The source was not found, but some or all event logs could not be searched. To create the source, you need permission to read all event logs to make sure that the new source name is unique. Inaccessible logs: Security.”

But all your users will see is something like: –

App crash when trying to write to Event Log

App crash when trying to write to Event Log

If you need to use the event log (which is good practice), then make sure you have created your source during your install and not during your exception handling.

As for the registry, unless your app is running in elevated mode, the only hive you now have access to is HKEY_CURRENT_USER, some of which itself may have been locked down, by specific apps to prevent changes. So if you need to read from HKEY_LOCAL_MACHINE then you should not be using CreateSubKey; but creating your keys/valuse during or app elevation and using OpenSubKey for reading… remember you exception capturing though, similar to Environment Variables, you’ll get an exception if you don’t have access or the key does not exist.

Windows SDK for Windows 7 and .NET Framework 4 on a Windows 8 Pro Dev Box

Ran into an issue today trying to install “Windows SDK for Windows 7 and .NET Framework 4”  (http://www.microsoft.com/en-gb/download/details.aspx?id=8279) on my Windows 8 Pro Dev Box.

The error given is: –

A problem occurred while installing selected Windows SDK components.

Installation of the “Microsoft Windows SDK for Windows 7” product has reported the following error: Please refer to Samples\Setup\HTML\ConfigDetails.htm document for further information.

Please attempt to resolve the problem and then start Windows SDK setup again. If you continue to have problems with this issue, please visit the SDKteam support page at http://go.microsoft.com/fwlink/?LinkId=130245.

Click the View Log button to review the installation log.
To exit, click Finish.

and looks like: –

Windows SDK for Windows 7 and .NET Framework 4 error message on Windows 8 Pro system

Windows SDK for Windows 7 and .NET Framework 4 error message on Windows 8 Pro system

Microsoft Visual C++ 2010 Redistributable

Before it tries to install it warns you that the “Microsoft Visual C++ 2010 Redistributable – 10.0.40219” is installed, is newer than the one included and so will not be updated… however this is the cause of the failed install…

Before installing you need to remove all C++ 2010 components.

Note that this issue is not unique to Windows 8, also affects Windows 7 and probably Windows XP too, though not tried it there.

You can also install OK, if during the component selection process you de-select “Microsoft Visual C++ 2010” under  “Redistributable Packages” and “Visual C++ Compilers” under “Windows Native Code Development“: –

Windows SDK for Windows 7 and .NET Framework 4 Component Selection

Windows SDK for Windows 7 and .NET Framework 4 – de-select C++ bits…

Meet the new ASP.NET and Web Tools 2012.2 release…

ASP.NET tooling gets a little better, with updates to Page Inspector, IntelliSense, Publishing and the Editor; Along with a batch of updated project Templates. See: –


for a short-ish (~25min) video on the details.

You can get the ASP.NET and Web Tools 2012.2 installer at: –



Some free Microsoft Press eBooks…

Currently available books include – Introducing SQL Server 2012, Introducing Windows Server 2012, Programming Windows 8 Apps, Introducing Windows 8 for IT Professionals, Understanding Virtualization Solutions and more… Available in PDF, Mobi (Kindle) and epub, you can check them out at: –


Introducing SQL Server 2012   Programming Windows 8 Apps   Introducing Windows Server 2012

.NET Framework Documentation Improvements

.NET Documentation Improvements“The CLR documentation team has been busy responding to feedback and making updates and changes to the .NET Framework documentation in the MSDN Library. We would like to tell you about the most recent set of document updates, which were published earlier in February.”


Application Lifecycle Management (ALM) – Visual Studio 2012 Update 1

For anyone who has missed it and would like play, or would like the updated version the Application Lifecycle Management (ALM) Virtual Machine and Hands-on-Labs from the ALM team at Microsoft, who originally published the VM at the tail end of 2011 for VS 2010. They have updated it to Visual Studio 2012 Update 1 in Dec 2012…

Free download, and well worth looking at… really need to find more time myself to have a dig into this one 🙂

NOTE: you need access to Hyper-V to use this VM, it is x64 and a Hyper-V image so will not run under Microsoft Virtual Machine, as that is x86 only; will probably run under VM Virtualbox or others that support x64. See the ALM blog for full details…


Hands On LabsThis virtual machine and corresponding hands-on-labs / demo scripts have been upgraded to use Visual Studio 2012 Update 1. One new lab was added, focusing on diagnosing and testing SharePoint 2010 applications with the new ALM tools available in Visual Studio 2012 Update 1. All other labs were revised slightly based on Update 1, with two labs gaining new exercises which demonstrate Update 1 capabilities. New and updated labs are listed in the table below

New features in Visual Studio 2012:

  • Agile Project Management in Visual Studio Team Foundation Server 2012
  • Building the Right Software – Generating Storyboards and Collecting Stakeholder Feedback with Visual Studio 2012
  • Diagnosing Issues in Production with IntelliTrace and Visual Studio 2012
  • Unit Testing, Code Coverage, and Code Clone Analysis with Visual Studio 2012 UPDATED for Update 1
  • Making Developers More Productive with Visual Studio Team Foundation Server 2012
  • Exploratory Testing and Other Enhancements in Microsoft Test Manager 2012
  • Introduction to PreEmptive Analytics
  • Lab Management Improvements in Visual Studio 2012
  • Testing and Debugging SharePoint 2010 Applications with Visual Studio 2012 NEW for Update 1
Labs upgraded from Visual Studio 2010:
  • Authoring and Running Manual Tests using Microsoft Test Manager 2012
  • Introduction to Test Planning with Microsoft Test Manager 2012
  • Introduction to Test Case Management with Microsoft Test Manager 2012
  • Introduction to Platform Testing with Microsoft Test Manager 2012
  • Introduction to Coded UI Tests with Visual Studio Ultimate 2012
  • Branching and Merging Visualization with Visual Studio Team Foundation Server 2012
  • Debugging with IntelliTrace using Visual Studio Ultimate 2012
  • Using Code Analysis with Visual Studio 2012 to Improve Code Quality
  • Understanding Class Coupling with Visual Studio Ultimate 2012
  • Code Discovery using the Architecture Tools in Visual Studio Ultimate 2012 UPDATED for Update 1
  • Using the Architecture Explorer in Visual Studio Ultimate 2012 to Analyze Your Code
  • Introduction to Web Performance and Load Testing with Visual Studio Ultimate 2012

Visual Studio 2012 Update 2 CTP

Some nice ALM goodies coming up in the Visual Studio 2012 Update 2 – the “HTML and JavaScript Visual Profiler” looks good – the February CTP is available now for those interested in an advance peek.



LINQ to SQL and Entity Framework… issues I have encountered with EF.

For the past 6 months or so there has been various discussions raised on the LINQ to SQL (released with .NET 3.5) vs. Entity Framework (released with .NET 3.5 SP1). Including several blog posts claiming that LINQ to SQL is dead (here, here and here are a few of them) and that LINQ to SQL is to be replaced by EF; these blogs were a result of an ADO.NET team blog post.

This is probably in part to do with the fact that LINQ to SQL was written by the C# development team, and EF is developed by the ADO.NET team, and they were both developed in parallel.

The ADO.NET team started development of the Entity Framework in 2003; at around the same time the C# design group embarked on developing a LINQ to SQL extension as a “stand-in” for the abandoned ObjectSpaces O/RM tool. I have read that LINQ to SQL was mainly written to help aid the development of LINQ in particular for the AND/OR query clauses.

Apparently the two projects were developed with little communication between the teams and in 2006 the ADO.NET team gained ownership of the LINQ to SQL and LINQ to DataSet implementations, adding LINQ to Entities into the mix.

The Entity Framework was originally meant to be included in .NET 3.5, however it was removed from early betas. It was later included in .NET 3.5 SP1. LINQ to SQL was originally developed to work with other databases, but prior to it’s release (for non-technical reasons) it was restricted to work with MS SQL Server only.

Having been playing with LINQ to SQL for a bit I found it very easy to implement and program against. Pretty much just dragging my tables into a DBML and by customising the MySettings class it allowed me to use the same connection settings as previous datasets and non-.NET apps. It made it very easy to integrate some LINQ to SQL with existing application development.

The database I am currently developing with has 364 tables (with around 850 FK links between them). Though dropping all of these onto a LINQ to SQL DBML file takes a while for Visual Studio to process, it works. Customising the DBML would be a bit of a pain however, given the lack of update facility in the designer (though there is a tool I am looking at http://www.huagati.com/dbmltools/, which would also allow me to standardise legacy naming conventions).

Would be nice to be able to spread the tables across several DBML files, allowing commonly used tables to be included in each LINQ to SQL class. Not had much chance to look at this but seems to cause problems when just dropping the tables into separate files.

Dropping the same tables into an Entity Data Model takes around the same time, but crucially any time I attempt to update the diagram in the designer, or often just selecting a table, Visual Studio bombs out, crashing.

There are bound to be several benefits to using the EF, such as: – 

  • Full provider model with support for multiple RDBMSs, including SQL Server Compact.
  • Not tied to a one:one relationship between entities and database tables.
  • Support for Table per Class and Table per Concrete Class hierarchy models.
  • Support for entity-splitting and complex types.

Though it may be the most serious issue I have encountered, this is not the only issue I have had with EF.

One common thing I do through-out code is check and see if changes have been made to a dataset or LINQ to SQL model. something easily achieved in both (<context>.GetChangeSet.Deletes, etc in LINQ to SQL and <DataTable>.GetChanges for DataTables).

It took me a fair amount of research to find how to do it for an Entity Data Model… and disappointingly it is not as simple a command: –

Dim changes  = _
     <context>.ObjectStateManager.GetObjectStateEntries( _
            EntityState.Added Or _
            EntityState.Modified Or _
EntityState.Deleted  _
            ).[Select](Function(o) o.Entity).OfType( _
                  Of <my table>)

Another problem I have had is to do with the connection settings. When you use an Entity Data Model, the connection string is not added to the MySettings class, it is just added to app.config. This make it a bit more involved to share connection settings in a common method (as discussed here). Plus when you go into the entity model, the connection setting is not selectable (or editable) in the design view.

Overall both are a good base and are much improved over having to write my own custom functions to build DataSets and DataAdapter from the large database schema. With both LINQ to SQL and LINQ to Entity, I can get past having to embed query strings in a DataAdpter. Building some classes that achieve the same thing but allow them to be debugged and syntax checked by Visual Studio when a schema update occurs.

As for which to use… Entity Framework looks to be the focus of future Microsoft developments in Visual Studio 2010, but in Visual Studio 2008 I like LINQ to SQL because it seems more integrated with the development environment, development is easier and it doesn’t crash Visual Studio! I do hope that the Entity Framework issues are fixed in .NET 4.0.

For now I will be continuing with LINQ to SQL, but ensuring it is separated out as much as I can, with as little customisation as possible, so that if need it can be easily replaced by Entity Data Models in the future.

Paul B

PS Eric Nelson posted a small blog and slides on Entity framework (Battle of the ORMs slides and links) today at http://geekswithblogs.net/iupdateable/archive/2009/02/16/battle-of-the-orms-slides-and-links.aspx.

Customising MySettings class in .NET to override a connection string.

With .NET 2.0 came the “My” classes, a useful feature of the classes was the integration into the “app.config” file and project properties for configuration of settings, including connection strings.

In order to maintain compatibility with previous app that share database configuration settings with new modules and developments, it would be nice to be able to override settings. It would be nice to use customised connection settings yet seamlessly use built in features of Visual Studio to set connection strings for DataAdapters, LINQtoSQL, etc.

Thankfully this is easily achieved in .NET 2.0 and onwards (with the exception of Entity Models, released with .NET 3.5 SP1, which I will go into in another blog).

To modify simply open the project properties, and switch to the the settings window and click on the “View Code” button.

This will add a partial class for “MySettings” to your project. From which it is easy to override any settings.

To override a particular property or group of properties is easy by overriding the “Item” property: –

Partial Friend NotInheritable Class MySettings
    Default Public Overloads Overrides Property Item(ByVal propertyName As String) As Object
propertyName = “MyDatabase”

End If
        End Get
(ByVal value As Object)
            If propertyName = “MyDatabase” Then

                MyBase.Item(propertyName) = value

            End If

        End Set
    End Property
End Class

Where “Connection” is a custom class build to use shared legacy application connection settings.

Paul B

Customizing Visual Studio 2008 Prerequisites to allow for Visual C++ 2005 Runtime

Ran into a problem a while back after upgrading to Visual Studio 2008 and thought I would share it.

When upgrading a group of projects that included an installer project which in turn includes the Visual C++ Runtime (2005) as a prerequisite, to Visual Studio 2008, it is not immediately obvious that the Visual C++ Runtime included in the prerequisites for Visual Studio 2008, is the 2008 version only.

The application I am currently working on uses a third party component whose prerequisite include the Visual C++ 2005 Runtime, so it is not under my control which version it needs. Components developed for C++ 2005 will not register with the C++ 2008 Runtime installed.

In order to include the 2005 runtime in the prerequisites list within you must add them in as custom prerequisites for Visual Studio 2008 installers.

This requires getting a copy of the Visual C++ 2005 Runtime installers and placing them in the Visual Studio 2008 folder (or vice versa). You will need to rename the folders as the folders and file names for 2005 and 2008 C++ Runtimes are the same. Once in the folder they will also need modified so that you can tell which are which (2005 or 2008), you do this by modifying the "package.xml" file held in the "en" folder of each package.

Once modified Visual Studio will pick up the new list and allow you to select the correct version.

Visual Studio 2008’s packages folder has changed and is now in the SDKs folder, on 32bit OS systems this will be "C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages" and on 64bit OS systems it is "C:\Program Files (x86)\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages" (default paths).

For Visual Studio 2005 the packages folder is in a sub folder of the install path, on 32bit OS systems this is "C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\BootStrapper\Packages" and on 64bit systems it is "C:\Program Files (x86)\Microsoft Visual Studio 8\SDK\v2.0\BootStrapper\Packages" (default paths).

In case you do not have access to the packages for the previous versions, I have uploaded them to my SkyDrive, click here to download.

Paul B