添加字体由CMD和C#
我写一个方法来安装cmd和C#的字体。 下面我的方法:添加字体由CMD和C#
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。 我的代码中有什么错误,或者做这个Job的更好方法是什么。 预先感谢您。
正如微软网站this question and answer你可以做到这一点通过复制字体文件夹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();
此代码拷贝一个文件,如果你有一个文件夹中的多个文件Get the font file in folder然后逐一复制。我测试这种方式,它工作正常。如果你有问题请评论回答。请注意,输出必须为以管理员身份运行。
使用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);
}
}
我尝试第一种方法,它的工作方式是: 1 - 当文件夹位于c目录中时,它无法复制它。如果我没有以管理员身份运行程序,它也不起作用。 非常感谢。 –
如果您以管理员身份运行Visual Studio,则无需以管理员身份运行它,您是否尝试过? –
以及关于c中的文件夹,代码是否出现错误? –
这可能是一个很大的可能,但你有“C:\ WINDOWS \字体” +“&REG”有你的字体路径之间没有空格和您的注册表命令。它可能的CMD将其解释为一条连续的路径。另外我不确定你可以将所有这些作为1个参数运行,你可能必须重定向输入并单独编写命令。这里是如何做到这一点链接https://*.com/questions/437419/execute-multiple-command-lines-with-the-same-process-using-net – Bearcat9425
我添加空间,但它不工作。 –
那么它的副本可能会将您的reg命令解释为交换机,而不理解要执行什么操作。看看我发布的链接,特别是显示如何重定向输入的链接,以便您可以运行多个命令,就像它们是同一进程的新命令一样。 – Bearcat9425