UserAppDataPath in WPF

Posted by psheriff on ASP.net Weblogs See other posts from ASP.net Weblogs or by psheriff
Published on Fri, 11 Mar 2011 19:17:00 GMT Indexed on 2011/03/12 0:11 UTC
Read the original article Hit count: 532

Filed under:
|
|

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:

C:\Users\PSheriff\AppData\Roaming\PDSA, Inc.\WindowsFormsApplication1\1.0.0.0

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.

C#
using System.Reflection;

public string GetUserAppDataPath()
{
  string path = string.Empty;
  Assembly assm;
  Type at;
  object[] r;

  // 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 =
                ((AssemblyCompanyAttribute)(r[0]));
  // Build the User App Data Path
  path = Environment.GetFolderPath(
              Environment.SpecialFolder.ApplicationData);
  path += @"\" + ct.Company;
  path += @"\" + assm.GetName().Version.ToString();

  return path;
}


Visual Basic
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 = _
                 DirectCast(r(0), AssemblyCompanyAttribute)
  ' Build the User App Data Path
  path = Environment.GetFolderPath( _
                Environment.SpecialFolder.ApplicationData)
  path &= "\" & ct.Company
  path &= "\" & assm.GetName().Version.ToString()

  Return path
End Function

Summary

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,
Paul Sheriff

** 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!

© ASP.net Weblogs or respective owner

Related posts about .NET

Related posts about wpf