2013-02-10 13 views
9

Wie kann ich eine Schriftart mit C# installieren?Wie installiere ich eine Windows-Schriftart mit C#

Ich habe versucht, die Schriftarten mit File.Copy() kopieren, aber ich bin nicht aufgrund von Zugriffsrechten Einschränkungen (UnauthorizedException) erlaubt.

Was soll ich tun?

+2

Ich bin mir ziemlich sicher, dass neue Schriften mehr Installation beinhaltet als nur die Dateien in den Ordner Fonts fertig zu werden. – dtb

+0

Hat das Erhöhen von Privilegien, unter denen die Anwendung läuft, das Problem gelöst? – abatishchev

+1

Sie sollten eine andere Frage stellen: "Wie installiere ich eine Schriftart?". Ihre vorhandene Frage hat eine triviale Antwort: Ihr Benutzer hat keine Zugriffsrechte. Diese Antwort hilft dir nicht. – usr

Antwort

18

Sie benötigen einen anderen Ansatz zur Installation von Schriftarten.

  • einen Installer verwenden (erstellen ein Setup-Projekt) die Schriften
  • Weitere (leichter) Ansatz zur Installation einer nativen Methode.

den Import DLL deklarieren:

[DllImport("gdi32.dll", EntryPoint="AddFontResourceW", SetLastError=true)] 
    public static extern int AddFontResource(
     [In][MarshalAs(UnmanagedType.LPWStr)] 
     string lpFileName); 

In Ihrem Code:

// Try install the font. 
    result = AddFontResource(@"C:\MY_FONT_LOCATION\MY_NEW_FONT.TTF"); 
    error = Marshal.GetLastWin32Error(); 

Die Quelle:

http://www.brutaldev.com/post/2009/03/26/Installing-and-removing-fonts-using-C

ich es zusammen in einem Gerät zu testen, Ich hoffe, das hilft:

[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); 
     } 
    } 
} 

, die Sie auf Ihrem Weg helfen sollten :)

+0

Kannst du mehr über "Declare the dll import" erklären: bcuz ich bin neu in C# –

+0

@ShahinRahbar bist du schon erfolgreich? Sie mussten die Antwort nicht im Voraus akzeptieren. Gib einfach die Antwort, wenn du damit zufrieden bist, akzeptiere die Antwort, wenn deine Frage tatsächlich gelöst ist. Lass es mich wissen, wenn du es hast, wenn nicht, ich werde versuchen, zu helfen – bas

+1

ich habe keine rep für upvote ... es kompilieren korrekt, aber Schriftart installiert nicht –

1
internal static void InstalarFuente(string NombreFnt,string RutaFnt) 
    { 
     string CMD = string.Format("copy /Y \"{0}\" \"%WINDIR%\\Fonts\" ", RutaFnt); 
     EjecutarCMD(CMD); 

     System.IO.FileInfo FInfo = new System.IO.FileInfo(RutaFnt); 
     CMD = string.Format("reg add \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts\" /v \"{0}\" /t REG_SZ /d {1} /f", NombreFnt, FInfo.Name); 
     EjecutarCMD(CMD); 
    } 

    public static void EjecutarCMD(string Comando) 
    { 
     System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo("cmd.exe"); 
     Info.Arguments = string.Format("/c {0}", Comando); 
     Info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
     System.Diagnostics.Process.Start(Info); 
    } 
+0

Abmelden, um die Schriftart zu berücksichtigen – JxDarkAngel