Created
May 20, 2011 15:06
-
-
Save trbngr/983108 to your computer and use it in GitHub Desktop.
Get Local Application Data Directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Reflection; | |
namespace ConsoleApplication1 | |
{ | |
internal class Program | |
{ | |
private static void Main() | |
{ | |
Assembly assembly = typeof(Program).Assembly; | |
var directoryNoVersion = LocalDataDirectory.FromAssembly(assembly); | |
Console.Out.WriteLine(directoryNoVersion.FullName); | |
var directoryWithVersion = LocalDataDirectory.FromAssembly(assembly, true); | |
Console.Out.WriteLine(directoryWithVersion.FullName); | |
//get file path | |
Console.Out.WriteLine(directoryWithVersion.GetFilePath("OurDatabase.mdf")); | |
Console.ReadKey(true); | |
} | |
} | |
public class LocalDataDirectory | |
{ | |
private readonly DirectoryInfo info; | |
private LocalDataDirectory(string path) | |
{ | |
info = !Directory.Exists(path) ? Directory.CreateDirectory(path) : new DirectoryInfo(path); | |
} | |
public DirectoryInfo Info | |
{ | |
get { return info; } | |
} | |
public string FullName | |
{ | |
get { return info.FullName; } | |
} | |
public string GetFilePath(string fileName) | |
{ | |
return Path.Combine(info.FullName, fileName); | |
} | |
public static LocalDataDirectory FromAssembly(Assembly assembly, bool useVersion = false) | |
{ | |
var company = assembly.GetAttribute<AssemblyCompanyAttribute>(); | |
var product = assembly.GetAttribute<AssemblyProductAttribute>(); | |
var version = assembly.GetAttribute<AssemblyFileVersionAttribute>(); | |
return new LocalDataDirectory(GetPath(useVersion, version, product, company)); | |
} | |
private static string GetPath(bool useVersion, AssemblyFileVersionAttribute version, AssemblyProductAttribute product, | |
AssemblyCompanyAttribute company) | |
{ | |
string appData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); | |
string path = Path.Combine(appData, company.Company, product.Product); | |
return !useVersion ? path : Path.Combine(path, version.Version); | |
} | |
} | |
public static class ExtendAssembly | |
{ | |
private static readonly Func<Assembly, object[]> getAttributes = | |
((Func<Assembly, object[]>) (ass => ass.GetCustomAttributes(false))).Memoize(); | |
public static TAttribute GetAttribute<TAttribute>(this Assembly assembly) where TAttribute : Attribute | |
{ | |
object[] attributes = getAttributes(assembly); | |
return attributes.Where(o => o is TAttribute) | |
.Select(o => (TAttribute) o) | |
.Single(); | |
} | |
} | |
public static class ExtendFunc | |
{ | |
public static Func<TArg, TResult> Memoize<TArg, TResult>(this Func<TArg, TResult> function) | |
{ | |
return Memoize(function, new Dictionary<TArg, TResult>()); | |
} | |
public static Func<TArg, TResult> Memoize<TArg, TResult>(this Func<TArg, TResult> function, | |
IDictionary<TArg, TResult> cache) | |
{ | |
return key => cache.ContainsKey(key) ? cache[key] : (cache[key] = function(key)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment