【问题标题】:Add fonts by CMD and C#通过 CMD 和 C# 添加字体
【发布时间】:2017-12-30 03:55:37
【问题描述】:

我写了一个通过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" + "&REG 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。 我的代码中有什么错误,或者有什么更好的方法来完成这项工作。 先感谢您。

【问题讨论】:

  • 这可能很大,但你有 "C:\windows\Fonts" + "&REG" 你的字体路径和你的注册表命令之间没有空格。它可能的 CMD 将其解释为一条连续路径。此外,我不确定您是否可以将所有这些作为 1 个参数运行,您可能必须重定向输入并单独编写命令。这是一个关于如何做到这一点的链接stackoverflow.com/questions/437419/…
  • 我添加了空间,但它不起作用。
  • 那么副本很可能将您的 reg 命令解释为它的开关,而不知道该怎么做。查看我发布的链接,特别是显示如何重定向输入的链接,以便您可以运行多个命令,就像它们是同一进程的新命令一样。
  • 你能在启动进程时准确显示 cc 字符串是什么吗?
  • @PaulF 即 cc: /C 复制 C:\Folder\ C:\Windows\Fonts &REG ADD HKLM\Software\Microsoft\Windows_NT\CurrentVersion\Fonts /v Libertinage /t REG_SZ /d Libertinage .ttf /f &暂停

标签: c# cmd fonts registry


【解决方案1】:

作为微软网站中的this question and answer,您可以通过复制字体文件夹中的字体文件并将其添加到注册表中来实现:

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目录中时,它无法复制它。 2-如果我不以管理员身份运行程序,它也不起作用。非常感谢。
  • 如果你以管理员身份运行visual studio,你不需要以管理员身份运行,你试试看?
  • 我在发布项目时不需要强制用户以管理员身份运行它,我该怎么做。关于c中的文件夹,没有错误,但是当我检查字体是否存在时,它不存在;即它不能从文件夹 c 复制字体。
  • 关于以管理员身份运行请看this question
  • 阿里,当一个代码工作时,如果你有一个特殊情况它不能工作,你必须在你的计算机中寻找这种情况的原因,也许是你的C:/潜水的特殊许可,或者某些软件可以做到这一点,如果您将文件夹放在 c:/ 中,程序可以正常工作吗?
猜你喜欢
  • 2016-10-15
  • 2013-01-13
  • 2013-11-23
  • 1970-01-01
  • 2013-03-01
  • 2022-10-14
  • 1970-01-01
  • 2023-04-02
  • 2016-03-25
相关资源
最近更新 更多