我正在从我的C#WPF应用程序中将ttf文件安装到我的C:/ Windows / Fonts文件夹中。虽然正在安装,但我收到了System.AccessViolation Exception。我的代码如下:
int result = -1;
int error = 0;
var windowsDirectory = Environment.GetEnvironmentVariable("SystemRoot") + "\\Fonts\\";
var directoryInfo = new DirectoryInfo("../../Assets/Fonts");
foreach (var file in directoryInfo.GetFiles())
{
result = AddFontResource((new FileInfo(windowsDirectory + file.Name)).ToString());
error = Marshal.GetLastWin32Error();
if (error != 0)
{
System.Diagnostics.Debug.WriteLine(new Win32Exception(error).Message);
}
else
{
System.Diagnostics.Debug.WriteLine((result == 0) ? "Font is already installed." :
"Font installed successfully.");
}
}
如何解决我的问题