C# 通过CMD和C添加字体#
我编写了一个通过cmd和C#安装字体的方法。 我的方法如下:C# 通过CMD和C添加字体#,c#,cmd,fonts,registry,C#,Cmd,Fonts,Registry,我编写了一个通过cmd和C#安装字体的方法。 我的方法如下: void installFont(string fontsFolderPath, string fontName) { System.Diagnostics.Process process = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnost
void installFont(string fontsFolderPath, string fontName)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = "cmd.exe";
String cc = @"/C copy " + fontsFolderPath + @" C:\Windows\Fonts" + "® ADD HKLM\\Software\\Microsoft\\Windows_NT\\CurrentVersion\\Fonts /v " + fontName + @" /t REG_SZ /d " + fontName + @".ttf /f &pause"; ;
startInfo.Arguments = cc;
process.StartInfo = startInfo;
process.Start();
}
但这不是添加它,我单独尝试了cmd指令,它工作正常,但当我通过C#请求它们时,它们不工作。
我以管理员身份运行VS。
我的代码中有什么错误,或者有什么更好的方法来完成这项工作。
先谢谢你 在Microsoft站点中,您可以通过复制字体文件夹中的te字体文件并将其添加到注册表中,如下所示:
File.Copy("BKoodakO.ttf", Path.Combine(GetFolderPath(SpecialFolder.Windows), "Fonts", "BKoodakO.ttf"),true);
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts");
key.SetValue("describtion for BKoodakO", "BKoodakO.ttf");
key.Close();
此代码复制一个文件,如果文件夹中有多个文件,则逐个复制。我用这种方法测试,效果很好。如果您有问题,请评论答案。请注意,输出必须以管理员身份运行
使用Windows dll执行此操作的另一种方法是:
[DllImport("gdi32", EntryPoint = "AddFontResource")]
public static extern int AddFontResourceA(string lpFileName);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern int AddFontResource(string lpszFilename);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern int CreateScalableFontResource(uint fdwHidden, string
lpszFontRes, string lpszFontFile, string lpszCurrentPath);
/// <summary>
/// Installs font on the user's system and adds it to the registry so it's available on the next session
/// Your font must be included in your project with its build path set to 'Content' and its Copy property
/// set to 'Copy Always'
/// </summary>
/// <param name="contentFontName">Your font to be passed as a resource (i.e. "myfont.tff")</param>
private static void RegisterFont(string contentFontName)
{
// Creates the full path where your font will be installed
var fontDestination = Path.Combine(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Fonts), contentFontName);
if (!File.Exists(fontDestination))
{
// Copies font to destination
System.IO.File.Copy(Path.Combine(System.IO.Directory.GetCurrentDirectory(), contentFontName), fontDestination);
// Retrieves font name
// Makes sure you reference System.Drawing
PrivateFontCollection fontCol = new PrivateFontCollection();
fontCol.AddFontFile(fontDestination);
var actualFontName = fontCol.Families[0].Name;
//Add font
AddFontResource(fontDestination);
//Add registry entry
Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts",
actualFontName, contentFontName, RegistryValueKind.String);
}
}
[DllImport(“gdi32”,EntryPoint=“addfontsource”)]
公共静态外部int ADDFONTROURCEA(字符串lpFileName);
[System.Runtime.InteropServices.DllImport(“gdi32.dll”)]
私有静态外部intaddFontResource(字符串lpszFilename);
[System.Runtime.InteropServices.DllImport(“gdi32.dll”)]
私有静态外部int CreateScalableFontResource(uint fdwHidden,字符串
lpszFontRes、字符串lpszFontFile、字符串lpszCurrentPath);
///
///在用户系统上安装字体并将其添加到注册表,以便在下一个会话中可用
///您的字体必须包含在项目中,且其生成路径设置为“内容”并具有“复制”属性
///设置为“始终复制”
///
///要作为资源传递的字体(即“myfont.tff”)
私有静态无效注册表项(字符串contentFontName)
{
//创建字体安装的完整路径
var fontDestination=Path.Combine(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Fonts),contentFontName);
如果(!File.Exists(fontDestination))
{
//将字体复制到目标
System.IO.File.Copy(Path.Combine(System.IO.Directory.GetCurrentDirectory(),contentFontName),fontDestination);
//检索字体名称
//确保参考系统。图纸
PrivateFontCollection fontCol=新PrivateFontCollection();
fontCol.AddFontFile(fontDestination);
var actualFontName=fontCol.Families[0]。名称;
//添加字体
AddFontResource(fontDestination);
//添加注册表项
Registry.SetValue(@“HKEY\U LOCAL\U MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts”,
actualFontName、contentFontName、RegistryValueKind.String);
}
}
这可能是一个很大的问题,但您有“C:\windows\Fonts”+“®”字体路径和注册表命令之间没有空格。它可能的指令是将其解释为一条连续路径。此外,我不确定您是否可以将所有这些作为一个参数运行,您可能必须重定向输入并单独编写命令。这里有一个关于如何添加空间的链接,但它不起作用。那么,复制可能是将您的reg命令解释为它的开关,而不知道该做什么。请看一下我发布的链接,特别是显示如何重定向输入的链接,以便您可以将多个命令像新命令一样运行到同一进程。启动进程时,您能否准确显示cc字符串。@PaulF这是cc:/C copy C:\Folder\C:\Windows\font®;ADDHKLM\Software\Microsoft\Windows\u NT\CurrentVersion\font/v Libertinage/t REG_SZ/d Libertinage.ttf/f&pausi尝试第一种方法,它是有效的,但是:1-当文件夹位于c目录中时,它无法复制它。2-如果我不是以管理员身份运行该程序,它也不起作用。非常感谢。如果您以管理员身份运行visual studio,则不需要以管理员身份运行它,是否尝试过?我不需要在发布项目时强制用户以管理员身份运行它,我如何才能做到这一点。关于c中的文件夹,并没有错误,但当我检查字体是否存在时,它不存在;i、 e.无法从文件夹c复制字体。关于以管理员身份运行请查看Ali,当代码工作时,如果您有一个特殊情况表明它不工作,您必须在您的计算机中查找该情况的原因,可能是您的c:/dive上有一个特殊许可证,或者某些软件不允许这样做,如果您将代码放入c:/中的文件夹中,程序工作正常吗?