我个人非常喜欢lance Larsen 和他的静态 AssemblyInfo 类的实现。
一个基本上将类粘贴到他的程序集中(我通常使用已经存在的 AssemblyInfo.cs 文件,因为它符合命名约定)
要粘贴的代码是:
internal static class AssemblyInfo
{
public static string Company { get { return GetExecutingAssemblyAttribute<AssemblyCompanyAttribute>(a => a.Company); } }
public static string Product { get { return GetExecutingAssemblyAttribute<AssemblyProductAttribute>(a => a.Product); } }
public static string Copyright { get { return GetExecutingAssemblyAttribute<AssemblyCopyrightAttribute>(a => a.Copyright); } }
public static string Trademark { get { return GetExecutingAssemblyAttribute<AssemblyTrademarkAttribute>(a => a.Trademark); } }
public static string Title { get { return GetExecutingAssemblyAttribute<AssemblyTitleAttribute>(a => a.Title); } }
public static string Description { get { return GetExecutingAssemblyAttribute<AssemblyDescriptionAttribute>(a => a.Description); } }
public static string Configuration { get { return GetExecutingAssemblyAttribute<AssemblyConfigurationAttribute>(a => a.Configuration); } }
public static string FileVersion { get { return GetExecutingAssemblyAttribute<AssemblyFileVersionAttribute>(a => a.Version); } }
public static Version Version { get { return Assembly.GetExecutingAssembly().GetName().Version; } }
public static string VersionFull { get { return Version.ToString(); } }
public static string VersionMajor { get { return Version.Major.ToString(); } }
public static string VersionMinor { get { return Version.Minor.ToString(); } }
public static string VersionBuild { get { return Version.Build.ToString(); } }
public static string VersionRevision { get { return Version.Revision.ToString(); } }
private static string GetExecutingAssemblyAttribute<T>(Func<T, string> value) where T : Attribute
{
T attribute = (T)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(T));
return value.Invoke(attribute);
}
}
您添加一个使用系统;到文件的顶部,你很高兴。
对于我的应用程序,我使用这个类来设置/获取/使用我的本地用户设置:
internal class ApplicationData
{
DirectoryInfo roamingDataFolder;
DirectoryInfo localDataFolder;
DirectoryInfo appDataFolder;
public ApplicationData()
{
appDataFolder = new DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), AssemblyInfo.Product,"Data"));
roamingDataFolder = new DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),AssemblyInfo.Product));
localDataFolder = new DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), AssemblyInfo.Product));
if (!roamingDataFolder.Exists)
roamingDataFolder.Create();
if (!localDataFolder.Exists)
localDataFolder.Create();
if (!appDataFolder.Exists)
appDataFolder.Create();
}
/// <summary>
/// Gets the roaming application folder location.
/// </summary>
/// <value>The roaming data directory.</value>
public DirectoryInfo RoamingDataFolder => roamingDataFolder;
/// <summary>
/// Gets the local application folder location.
/// </summary>
/// <value>The local data directory.</value>
public DirectoryInfo LocalDataFolder => localDataFolder;
/// <summary>
/// Gets the local data folder location.
/// </summary>
/// <value>The local data directory.</value>
public DirectoryInfo AppDataFolder => appDataFolder;
}
看看 Larsens 网站 (MVP),他有很酷的东西可以从中汲取灵感。