0

I am installing a ttf file into my C:/Windows/Fonts folder from my C# WPF application.While installing I am getting System.AccessViolation Exception. My code is below:

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

How do Iresolve my issue

user2877090
  • 73
  • 2
  • 7
  • Have you looked up the error type on MSDN? It's very clear what the issue is after you read up on it. – Brian Driscoll Mar 28 '14 at 13:37
  • i know what the issue is. I am trying to write to C:/Windows/Fonts folder which needs administrative rights. I am asking is there a way to resolve it directly from the WPF C# code – user2877090 Mar 28 '14 at 13:42
  • possible duplicate of [How to force my .NET App to run as administrator on Windows 7?](http://stackoverflow.com/questions/2818179/how-to-force-my-net-app-to-run-as-administrator-on-windows-7) – Brian Driscoll Mar 28 '14 at 13:54

1 Answers1

0

In case the exception is really based on missing administrator rights, you might want to read this articel about how to set up your app config for administrator rights.

Community
  • 1
  • 1
SnowballTwo
  • 529
  • 2
  • 11