In Windows Forms applications you were able to get to your user's roaming profile directory very easily using the Application.UserAppDataPath property. This folder allows you to store information for your program in a custom folder specifically for your program. The format of this directory looks like this:
C:\Users\YOUR NAME\AppData\Roaming\COMPANY NAME\APPLICATION NAME\APPLICATION VERSION
For example, on my Windows 7 64-bit system, this folder would look like this for a Windows Forms Application:
For some reason Microsoft did not expose this property from the Application object of WPF applications. I guess they think that we don't need this property in WPF? Well, sometimes we still do need to get at this folder.
You have two choices on how to retrieve this property. Add a reference to the System.Windows.Forms.dll to your WPF application and use this property directly. Or, you can write your own method to build the same path. If you add a reference to the System.Windows.Forms.dll you will need to use System.Windows.Forms.Application.UserAppDataPath to access this property.
Create a GetUserAppDataPath Method in WPF
If you want to build this path you can do so with just a few method calls in WPF using Reflection. The code below shows this fairly simple method to retrieve the same folder as shown above.
public string GetUserAppDataPath()
string path = string.Empty;
// Get the .EXE assembly
assm = Assembly.GetEntryAssembly();
// Get a 'Type' of the AssemblyCompanyAttribute
at = typeof(AssemblyCompanyAttribute);
// Get a collection of custom attributes from the .EXE assembly
r = assm.GetCustomAttributes(at, false);
// Get the Company Attribute
AssemblyCompanyAttribute ct =
// Build the User App Data Path
path = Environment.GetFolderPath(
path += @"\" + ct.Company;
path += @"\" + assm.GetName().Version.ToString();
Public Function GetUserAppDataPath() As String
Dim path As String = String.Empty
Dim assm As Assembly
Dim at As Type
Dim r As Object()
' Get the .EXE assembly
assm = Assembly.GetEntryAssembly()
' Get a 'Type' of the AssemblyCompanyAttribute
at = GetType(AssemblyCompanyAttribute)
' Get a collection of custom attributes from the .EXE assembly
r = assm.GetCustomAttributes(at, False)
' Get the Company Attribute
Dim ct As AssemblyCompanyAttribute = _
' Build the User App Data Path
path = Environment.GetFolderPath( _
path &= "\" & ct.Company
path &= "\" & assm.GetName().Version.ToString()
Getting the User Application Data Path folder in WPF is fairly simple with just a few method calls using Reflection. Of course, there is absolutely no reason you cannot just add a reference to the System.Windows.Forms.dll to your WPF application and use that Application object. After all, System.Windows.Forms.dll is a part of the .NET Framework and can be used from WPF with no issues at all.
NOTE: Visit http://www.pdsa.com/downloads to get more tips and tricks like this one.
Good Luck with your Coding,
** SPECIAL OFFER FOR MY BLOG READERS **
We frequently offer a FREE gift for readers of my blog. Visit http://www.pdsa.com/Event/Blog for your FREE gift!