C# 产品注册表信息
namespace System
{
using System.Globalization;
using System.Text;
using Microsoft.Win32;
[System.Diagnostics.CodeAnalysis.SuppressMessage("", "S3903")]
static class ProductRegistryValue
{
private const string ProductRegistrySubKey = @"SOFTWARE\{Company}\{UserDataAppName}";
private static readonly Version minVersion = new Version(0, 0, 0, 0);
public static string GetProductRegistryValue(RegistryKey rootRegistry, string registryName)
{
return GetRegistryValue(rootRegistry, ProductRegistrySubKey, registryName);
}
public static string GetProductRegistryValueWOW6432(RegistryKey rootRegistry, string registryName)
{
var registrySubKey = GetRegistrySubKeyByRoot(ProductRegistrySubKey);
return GetRegistryValue(rootRegistry, registrySubKey, registryName);
}
public static void SetProductRegistryValue(RegistryKey rootRegistry, string registryName, string value)
{
SetRegistryValue(rootRegistry, ProductRegistrySubKey, registryName, value);
}
public static void SetProductRegistryValueWOW6432(RegistryKey rootRegistry, string registryName, string value)
{
var registrySubKey = GetRegistrySubKeyByRoot(ProductRegistrySubKey);
SetRegistryValue(rootRegistry, registrySubKey, registryName, value);
}
private static string GetRegistrySubKeyByRoot(string registrySubKey)
{
if (GetRegistrySubKeyByRootItem("SOFTWARE", @"\WOW6432Node", ref registrySubKey))
{
}
return registrySubKey;
}
private static bool GetRegistrySubKeyByRootItem(string begin, string add, ref string registrySubKey)
{
bool needAdd = registrySubKey.StartsWith(begin, StringComparison.OrdinalIgnoreCase);
if (needAdd)
{
var builder = new StringBuilder(registrySubKey.Length + 64);
builder.Append(registrySubKey.Substring(0, begin.Length));
builder.Append(add);
builder.Append(registrySubKey.Substring(begin.Length));
registrySubKey = builder.ToString();
}
return needAdd;
}
public static string GetRegistryValue(RegistryKey registry, string registrySubKey, string registryName)
{
using (RegistryKey subKey = registry.OpenSubKey(registrySubKey))
{
string value = subKey?.GetValue(registryName)?.ToString();
return value;
}
}
private static void SetRegistryValue(RegistryKey registry, string registrySubKey, string registryName, string value)
{
using (RegistryKey subKey = registry.CreateSubKey(registrySubKey, RegistryKeyPermissionCheck.ReadWriteSubTree))
{
if (subKey != null)
{
subKey.SetValue(registryName, value);
}
}
}
public static Version GetVersion(string versionString)
{
return GetVersion(versionString, minVersion);
}
public static Version GetVersion(string versionString, Version defaultVersion)
{
Version version;
try
{
if (string.IsNullOrEmpty(versionString))
{
version = defaultVersion;
}
else
{
version = new Version(versionString);
}
}
catch (Exception ex)
{
version = defaultVersion;
TraceException($"Get version by string exception", ex);
}
return version;
}
public static bool IsNullOrEmpty(Version version)
{
return (version == null) ||
(version.Major == 0 && version.Minor == 0 && version.Build == 0 && version.Revision == 0);
}
public static DateTime GetLocalDateTime(string timeString, DateTime defaultTime)
{
DateTime dateTime;
try
{
if (!string.IsNullOrEmpty(timeString) &&
DateTime.TryParse(timeString, DateTimeFormatInfo.InvariantInfo,
DateTimeStyles.None, out dateTime))
{
switch (dateTime.Kind)
{
case DateTimeKind.Unspecified:
dateTime = new DateTime(dateTime.Ticks, DateTimeKind.Local);
break;
case DateTimeKind.Utc:
dateTime = dateTime.ToLocalTime();
break;
}
}
else
{
dateTime = defaultTime;
}
}
catch (Exception ex)
{
dateTime = defaultTime;
TraceException($"Get date time by string exception", ex);
}
return dateTime;
}
public static string GetLocalDateTimeString(DateTime dateTime)
{
var safeTicks = dateTime.Ticks;
var safeKind = dateTime.Kind;
if (!(safeTicks == 0 && safeKind == DateTimeKind.Unspecified) &&
safeKind != DateTimeKind.Local)
{
dateTime = dateTime.ToLocalTime();
}
return dateTime.ToString("s", DateTimeFormatInfo.InvariantInfo);
}
private static void TraceException(string message, Exception ex)
{
System.Diagnostics.Trace.Write(message);
System.Diagnostics.Trace.WriteLine(ex);
}
}
}