如何使用C#安装Windows字体

9

我该如何使用C#安装字体?

我尝试使用File.Copy()复制字体,但由于权限限制(UnauthorizedException)而无法进行。

我应该怎么办?


2
我相信安装新字体不仅仅涉及将文件复制到字体文件夹中。 - dtb
提升应用程序运行权限可以解决问题吗? - abatishchev
1
你应该问一个不同的问题:“如何安装字体?”你现有的问题有一个琐碎的答案:你的用户没有访问权限。这个答案并不能帮助你。 - usr
@usr 是的,我也同意,刚刚完成了编辑问题标题的工作,一旦编辑得到同行审查,更改就会反映出来。 - Baljeetsingh Sucharia
2个回答

18

你需要采用不同的方法安装字体。

  • 使用安装程序(创建安装项目)来安装字体
  • 另一种更简单的方法是使用本地方法。

声明 DLL 导入:

    [DllImport("gdi32.dll", EntryPoint="AddFontResourceW", SetLastError=true)]
    public static extern int AddFontResource(
        [In][MarshalAs(UnmanagedType.LPWStr)]
        string lpFileName);

在你的代码中:

    // Try install the font.
    result = AddFontResource(@"C:\MY_FONT_LOCATION\MY_NEW_FONT.TTF");
    error = Marshal.GetLastWin32Error();

资源:

http://www.brutaldev.com/post/2009/03/26/使用C安装和删除字体

我已经在单元测试中整合了它,希望这有所帮助:

[TestFixture]
public class Tests
{
    // Declaring a dll import is nothing more than copy/pasting the next method declaration in your code. 
    // You can call the method from your own code, that way you can call native 
    // methods, in this case, install a font into windows.
    [DllImport("gdi32.dll", EntryPoint = "AddFontResourceW", SetLastError = true)]
    public static extern int AddFontResource([In][MarshalAs(UnmanagedType.LPWStr)]
                                     string lpFileName);

    // This is a unit test sample, which just executes the native method and shows
    // you how to handle the result and get a potential error.
    [Test]
    public void InstallFont()
    {
        // Try install the font.
        var result = AddFontResource(@"C:\MY_FONT_LOCATION\MY_NEW_FONT.TTF");
        var error = Marshal.GetLastWin32Error();
        if (error != 0)
        {
            Console.WriteLine(new Win32Exception(error).Message);
        }
    }
}

那应该能帮助你继续前进 :)


你能解释一下“声明dll导入”吗?因为我是C#新手。 - Shahin Rahbar
@ShahinRahbar 你成功了吗?你不必事先接受答案。如果你对答案满意,只需点赞即可,当你的问题真正解决时再接受答案。如果你还没有解决问题,请告诉我,我会尽力帮助。 - bas
1
我没有足够的声望来点赞...它编译正确,但字体没有安装。 - Shahin Rahbar
系统找不到指定的文件。 - Shahin Rahbar
也许现在是创建一个新问题的时候了,您可以分享足够的信息,以便SO社区能够帮助您。复制/粘贴您目前拥有的代码,并在新问题中共享错误+堆栈跟踪。 - bas
显示剩余3条评论

1
   internal static void InstalarFuente(string NombreFnt,string RutaFnt)
    {
        string CMD = string.Format("copy /Y \"{0}\" \"%WINDIR%\\Fonts\" ", RutaFnt);
        EjecutarCMD(CMD);

        System.IO.FileInfo FInfo = new System.IO.FileInfo(RutaFnt);
        CMD = string.Format("reg add \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts\" /v \"{0}\" /t REG_SZ /d {1} /f", NombreFnt, FInfo.Name);
        EjecutarCMD(CMD);
    }

    public static void EjecutarCMD(string Comando)
    {
        System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo("cmd.exe");
        Info.Arguments = string.Format("/c {0}", Comando);
        Info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        System.Diagnostics.Process.Start(Info);
    }

注销以考虑字体。 - JxDarkAngel
请问,您是否了解将文件复制到Windows目录的“超过用户”权限? - S.H.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接