字体预览工具 v1.0.0 初始版本
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security.Principal;
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace FontPreview;
|
||||
|
||||
internal static class FontManagerEx
|
||||
{
|
||||
internal static bool IsAdmin()
|
||||
{
|
||||
try
|
||||
{
|
||||
using WindowsIdentity ntIdentity = WindowsIdentity.GetCurrent();
|
||||
WindowsPrincipal windowsPrincipal = new WindowsPrincipal(ntIdentity);
|
||||
return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
internal static string GetFontDisplayName(string fontPath)
|
||||
{
|
||||
return FontNameParser.GetDisplayName(fontPath, null);
|
||||
}
|
||||
|
||||
internal static bool InstallAsShortcut(string fontFullPath)
|
||||
{
|
||||
string lnkPath;
|
||||
string regName;
|
||||
return InstallAsShortcut(fontFullPath, out lnkPath, out regName);
|
||||
}
|
||||
|
||||
internal static bool InstallAsShortcut(string fontFullPath, out string lnkPath, out string regName)
|
||||
{
|
||||
lnkPath = "";
|
||||
regName = "";
|
||||
try
|
||||
{
|
||||
string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);
|
||||
string fileName = Path.GetFileName(fontFullPath);
|
||||
lnkPath = Path.Combine(folderPath, fileName + ".lnk");
|
||||
Type typeFromCLSID = Type.GetTypeFromCLSID(new Guid("00021401-0000-0000-C000-000000000046"));
|
||||
if (typeFromCLSID == null)
|
||||
{
|
||||
lnkPath = "";
|
||||
return false;
|
||||
}
|
||||
object obj = Activator.CreateInstance(typeFromCLSID);
|
||||
try
|
||||
{
|
||||
ShellLinkW shellLinkW = (ShellLinkW)obj;
|
||||
shellLinkW.SetPath(fontFullPath);
|
||||
shellLinkW.SetWorkingDirectory(Path.GetDirectoryName(fontFullPath));
|
||||
shellLinkW.SetIconLocation(fontFullPath, 0);
|
||||
PersistFile persistFile = (PersistFile)obj;
|
||||
persistFile.Save(lnkPath, fRemember: true);
|
||||
Marshal.FinalReleaseComObject(persistFile);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.FinalReleaseComObject(obj);
|
||||
}
|
||||
if (!File.Exists(lnkPath))
|
||||
{
|
||||
lnkPath = "(.lnk 生成失败)";
|
||||
return false;
|
||||
}
|
||||
try
|
||||
{
|
||||
using RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts");
|
||||
if (registryKey != null)
|
||||
{
|
||||
string fullPath = Path.GetFullPath(fontFullPath);
|
||||
bool flag = false;
|
||||
string[] valueNames = registryKey.GetValueNames();
|
||||
foreach (string name in valueNames)
|
||||
{
|
||||
object value = registryKey.GetValue(name);
|
||||
if (value is string)
|
||||
{
|
||||
string a = (string)value;
|
||||
if (string.Equals(a, fontFullPath, StringComparison.OrdinalIgnoreCase) || string.Equals(a, fullPath, StringComparison.OrdinalIgnoreCase) || string.Equals(a, fileName, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (flag)
|
||||
{
|
||||
regName = "";
|
||||
lnkPath = "(已安装,未重复登记)";
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
string text = GetFontDisplayName(fontFullPath);
|
||||
if (string.IsNullOrEmpty(text))
|
||||
{
|
||||
text = Path.GetFileNameWithoutExtension(fontFullPath);
|
||||
}
|
||||
regName = text + " (TrueType)";
|
||||
using (RegistryKey registryKey2 = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts", writable: true))
|
||||
{
|
||||
if (registryKey2 == null)
|
||||
{
|
||||
lnkPath = "(无法打开字体注册表键,需管理员权限)";
|
||||
return false;
|
||||
}
|
||||
registryKey2.SetValue(regName, fontFullPath, RegistryValueKind.String);
|
||||
}
|
||||
try
|
||||
{
|
||||
using RegistryKey registryKey3 = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts");
|
||||
string value2 = ((registryKey3 == null) ? null : (registryKey3.GetValue(regName) as string));
|
||||
if (string.IsNullOrEmpty(value2))
|
||||
{
|
||||
lnkPath = "(注册表项写入后未验证通过)";
|
||||
regName = "";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
regName = "";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
lnkPath = "(异常:" + ex.Message + ")";
|
||||
regName = "";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user