代码之家  ›  专栏  ›  技术社区  ›  Ali A. Jalil

按CMD和C添加字体#

  •  2
  • Ali A. Jalil  · 技术社区  · 8 年前

    我编写了一个通过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请求它们时,它们不工作。 我的代码中有什么错误,或者有什么更好的方法来完成这项工作。 提前谢谢你。

    1 回复  |  直到 8 年前
        1
  •  1
  •   Ali Hawk Rong    8 年前

    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 然后逐个复制。我用这种方法测试,效果很好。如果你有问题,请评论答案。注意,输出必须 .

            [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);
                }
            }