Skip to content

Instantly share code, notes, and snippets.

@mcxiaoke
Forked from jrgcubano/ApplicationInfo.cs
Created June 12, 2022 13:14
Show Gist options
  • Select an option

  • Save mcxiaoke/87294de8d7b2c755c8105a9f3eb3d3c3 to your computer and use it in GitHub Desktop.

Select an option

Save mcxiaoke/87294de8d7b2c755c8105a9f3eb3d3c3 to your computer and use it in GitHub Desktop.
Provide info about running application C#
/// <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> _userDataFolderById = new Lazy<string>(GetUserDataFolderById);
private static readonly Lazy<string> _userRoamingDataFolderById = new Lazy<string>(GetUserRoamingDataFolderById);
private static readonly Lazy<string> _allUsersDataFolderById = new Lazy<string>(GetAllUsersDataFolderById);
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);
private static readonly Lazy<string> _currentDomainConfigFile = new Lazy<string>(GetCurrentDomainConfigFile);
/// <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 by id
/// </summary>
public static string UserDataFolderById { get { return _userDataFolderById.Value; } }
/// <summary>
/// Get the current user roaming data folder by id
/// </summary>
public static string UserRoamingDataFolderById { get { return _userRoamingDataFolderById.Value; } }
/// <summary>
/// Get all users data folder by id
/// </summary>
public static string AllUsersDataFolderById { get { return _allUsersDataFolderById.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; } }
/// <summary>
/// Get current domain config file
/// </summary>
public static string CurrentDomainConfigFile { get { return _currentDomainConfigFile.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 GetUserDataFolderById()
{
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 GetUserRoamingDataFolderById()
{
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 GetAllUsersDataFolderById()
{
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 GetUserDataFolder()
{
string folderBase = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string path = string.Format(@"{0}\", folderBase);
return CreatePath(path);
}
private static string GetUserRoamingDataFolder()
{
string folderBase = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string path = string.Format(@"{0}\", folderBase);
return CreatePath(path);
}
private static string GetAllUsersDataFolder()
{
string folderBase = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
string path = string.Format(@"{0}\", folderBase);
return CreatePath(path);
}
private static string CreatePath(string path)
{
Directory.CreateDirectory(path);
return path;
}
private static string GetCurrentDomainConfigFile()
{
string path = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
return path;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment