2014-03-28 46 views
0

我從我的C#WPF應用程序安裝ttf文件到我的C:/ Windows/Fonts文件夾中。安裝時遇到System.AccessViolation異常。我的代碼如下:C#AccessViolationException

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."); 
       } 
      } 

如何Iresolve我的問題

+0

您是否在MSDN上查找了錯誤類型?你仔細閱讀後很清楚問題是什麼。 –

+0

我知道問題是什麼。我正在嘗試寫入需要管理權限的C:/ Windows/Fonts文件夾。我問有沒有辦法直接從WPF C#代碼 – user2877090

+0

解決它[如何強制我的.NET應用程序在Windows 7上以管理員身份運行?](http://stackoverflow.com/questions/2818179/)如何對力我淨的應用程序可以運行,作爲管理員,在窗口-7) –

回答

0

萬一例外,實在是基於管理員的權限,你可能需要閱讀this articel關於如何設置你的應用程序配置的管理員權限。

相關問題