Sha*_*bar 9 c# fonts access-denied unauthorized
如何使用C#安装字体?
我尝试使用复制字体,File.Copy()
但由于访问权限限制,我不被允许(UnauthorizedException
).
我该怎么办?
bas*_*bas 18
您需要一种不同的方法来安装字体.
声明dll导入:
[DllImport("gdi32.dll", EntryPoint="AddFontResourceW", SetLastError=true)]
public static extern int AddFontResource(
[In][MarshalAs(UnmanagedType.LPWStr)]
string lpFileName);
Run Code Online (Sandbox Code Playgroud)
在你的代码中:
// Try install the font.
result = AddFontResource(@"C:\MY_FONT_LOCATION\MY_NEW_FONT.TTF");
error = Marshal.GetLastWin32Error();
Run Code Online (Sandbox Code Playgroud)
来源:
http://www.brutaldev.com/post/2009/03/26/Installing-and-removing-fonts-using-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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这应该会帮助你的方式:)
归档时间: |
|
查看次数: |
13319 次 |
最近记录: |