Created
January 14, 2015 09:00
-
-
Save jrgcubano/213e211abcebe4d8937c to your computer and use it in GitHub Desktop.
Helper to get application information
This file contains hidden or 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.Reflection; | |
using System.IO; | |
using System; | |
using System.Runtime.InteropServices; | |
namespace PMS_UI.Infrastructure.Applications | |
{ | |
/// <summary> | |
/// This class provides information about the running application. | |
/// </summary> | |
public static class ApplicationInfo | |
{ | |
private static readonly Lazy<string> _productName = new Lazy<string>(GetProductName); | |
private static readonly Lazy<string> _version = new Lazy<string>(GetVersion); | |
private static readonly Lazy<string> _company = new Lazy<string>(GetCompany); | |
private static readonly Lazy<string> _copyright = new Lazy<string>(GetCopyright); | |
private static readonly Lazy<string> _applicationPath = new Lazy<string>(GetApplicationPath); | |
private static readonly Lazy<Guid> _applicationId = new Lazy<Guid>(GetApplicationId); | |
private static readonly Lazy<Guid> _assemblyId = new Lazy<Guid>(GetAssemblyId); | |
private static readonly Lazy<string> _userDataFolder = new Lazy<string>(GetUserDataFolder); | |
private static readonly Lazy<string> _userRoamingDataFolder = new Lazy<string>(GetUserRoamingDataFolder); | |
private static readonly Lazy<string> _allUsersDataFolder = new Lazy<string>(GetAllUsersDataFolder); | |
/// <summary> | |
/// Gets the product name of the application. | |
/// </summary> | |
public static string ProductName { get { return _productName.Value; } } | |
/// <summary> | |
/// Gets the version number of the application. | |
/// </summary> | |
public static string Version { get { return _version.Value; } } | |
/// <summary> | |
/// Gets the company of the application. | |
/// </summary> | |
public static string Company { get { return _company.Value; } } | |
/// <summary> | |
/// Gets the copyright information of the application. | |
/// </summary> | |
public static string Copyright { get { return _copyright.Value; } } | |
/// <summary> | |
/// Gets the path for the executable file that started the application, not including the executable name. | |
/// </summary> | |
public static string ApplicationPath { get { return _applicationPath.Value; } } | |
/// <summary> | |
/// Get the Application Guid | |
/// </summary> | |
public static Guid ApplicationId { get { return _applicationId.Value; } } | |
/// <summary> | |
/// Get the current assembly Guid. | |
/// <remarks> | |
/// Note that the Assembly Guid is not necessarily the same as the | |
/// Application Guid - if this code is in a DLL, the Assembly Guid | |
/// will be the Guid for the DLL, not the active EXE file. | |
/// </remarks> | |
/// </summary> | |
public static Guid AssemblyId { get { return _assemblyId.Value; } } | |
/// <summary> | |
/// Get the current user data folder | |
/// </summary> | |
public static string UserDataFolder { get { return _userDataFolder.Value; } } | |
/// <summary> | |
/// Get the current user roaming data folder | |
/// </summary> | |
public static string UserRoamingDataFolder { get { return _userRoamingDataFolder.Value; } } | |
/// <summary> | |
/// Get all users data folder | |
/// </summary> | |
public static string AllUsersDataFolder { get { return _allUsersDataFolder.Value; } } | |
private static string GetProductName() | |
{ | |
Assembly entryAssembly = Assembly.GetEntryAssembly(); | |
if (entryAssembly != null) | |
{ | |
AssemblyProductAttribute attribute = ((AssemblyProductAttribute)Attribute.GetCustomAttribute( | |
entryAssembly, typeof(AssemblyProductAttribute))); | |
return (attribute != null) ? attribute.Product : ""; | |
} | |
return ""; | |
} | |
private static string GetVersion() | |
{ | |
Assembly entryAssembly = Assembly.GetEntryAssembly(); | |
if (entryAssembly != null) | |
{ | |
return entryAssembly.GetName().Version.ToString(); | |
} | |
return ""; | |
} | |
private static string GetCompany() | |
{ | |
Assembly entryAssembly = Assembly.GetEntryAssembly(); | |
if (entryAssembly != null) | |
{ | |
AssemblyCompanyAttribute attribute = ((AssemblyCompanyAttribute)Attribute.GetCustomAttribute( | |
entryAssembly, typeof(AssemblyCompanyAttribute))); | |
return (attribute != null) ? attribute.Company : ""; | |
} | |
return ""; | |
} | |
private static string GetCopyright() | |
{ | |
Assembly entryAssembly = Assembly.GetEntryAssembly(); | |
if (entryAssembly != null) | |
{ | |
AssemblyCopyrightAttribute attribute = (AssemblyCopyrightAttribute)Attribute.GetCustomAttribute( | |
entryAssembly, typeof(AssemblyCopyrightAttribute)); | |
return attribute != null ? attribute.Copyright : ""; | |
} | |
return ""; | |
} | |
private static string GetApplicationPath() | |
{ | |
Assembly entryAssembly = Assembly.GetEntryAssembly(); | |
if (entryAssembly != null) | |
{ | |
return Path.GetDirectoryName(entryAssembly.Location); | |
} | |
return ""; | |
} | |
private static Guid GetApplicationId() | |
{ | |
Assembly asm = Assembly.GetEntryAssembly(); | |
object[] attr = (asm.GetCustomAttributes(typeof(GuidAttribute), true)); | |
return new Guid((attr[0] as GuidAttribute).Value); | |
} | |
private static Guid GetAssemblyId() | |
{ | |
Assembly asm = Assembly.GetExecutingAssembly(); | |
object[] attr = (asm.GetCustomAttributes(typeof(GuidAttribute), true)); | |
return new Guid((attr[0] as GuidAttribute).Value); | |
} | |
private static string GetUserDataFolder() | |
{ | |
Guid appGuid = ApplicationId; | |
string folderBase = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); | |
string path = string.Format(@"{0}\{1}\", folderBase, appGuid.ToString("B").ToUpper()); | |
return CreatePath(path); | |
} | |
private static string GetUserRoamingDataFolder() | |
{ | |
Guid appGuid = ApplicationId; | |
string folderBase = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); | |
string path = string.Format(@"{0}\{1}\", folderBase, appGuid.ToString("B").ToUpper()); | |
return CreatePath(path); | |
} | |
private static string GetAllUsersDataFolder() | |
{ | |
Guid appGuid = ApplicationId; | |
string folderBase = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); | |
string path = string.Format(@"{0}\{1}\", folderBase, appGuid.ToString("B").ToUpper()); | |
return CreatePath(path); | |
} | |
private static string CreatePath(string path) | |
{ | |
Directory.CreateDirectory(path); | |
return path; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment