Skip to content

Instantly share code, notes, and snippets.

@trbngr
Created May 20, 2011 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trbngr/983108 to your computer and use it in GitHub Desktop.
Save trbngr/983108 to your computer and use it in GitHub Desktop.
Get Local Application Data Directory
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