C#AccessViolationException

时间:2014-03-28 13:35:13

标签: c# wpf fonts

我正在从我的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.");
                }
            }

如何解决我的问题

1 个答案:

答案 0 :(得分:0)

如果异常确实基于缺少管理员权限,您可能需要阅读this articel有关如何设置应用配置以获取管理员权限的信息。