2013-02-10 35 views
9

Come posso installare un font usando C#?Come installare un font Windows usando C#

Ho provato a copiare i caratteri utilizzando File.Copy() ma non sono consentito a causa delle limitazioni dei diritti di accesso (UnauthorizedException).

Cosa devo fare?

+2

Sono abbastanza sicuro che l'installazione di nuovi font coinvolge più solo affrontare i file nella cartella Fonts. – dtb

+0

L'elevazione dei privilegi dell'applicazione è in esecuzione per risolvere il problema? – abatishchev

+1

Dovresti fare una domanda diversa: "Come installare un font?". La tua domanda esistente ha una risposta banale: il tuo utente non ha diritti di accesso. Questa risposta non ti aiuta. – usr

risposta

18

Avrete bisogno di un approccio diverso installando i font.

  • Utilizzare un programma di installazione (creare un progetto di installazione) per installare i font
  • Un altro approccio (più facile) utilizzando un metodo nativo.

dichiarare l'importazione dll:

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

Nel codice:

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

La fonte:

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

ho messo insieme in un test di unità, Spero che questo aiuti:

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

che dovrebbe aiutare nel vostro cammino :)

+0

Puoi spiegarci di più su "Dichiara l'importazione dll:" bcuz sono nuovo su C# –

+0

@ShahinRahbar hai avuto successo? Non dovevi accettare la risposta in anticipo. Rispondi semplicemente alla risposta se sei soddisfatto, accetta la risposta quando la tua domanda è effettivamente risolta. Fammi sapere quando ce l'hai, se no, cercherò di aiutare – bas

+1

non ho rep per upvote ... si compila correttamente ma il font non installa –

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

Disconnessione per prendere in considerazione il Font – JxDarkAngel