2009-06-09 12 views

cevap

25
using System; 
using System.Drawing.Printing; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 

     private void listAllPrinters() 
     { 
      foreach (var item in PrinterSettings.InstalledPrinters) 
      {  
       this.listBox1.Items.Add(item.ToString()); 
      } 
     } 

     private void listBox1_SelectedValueChanged(object sender, EventArgs e) 
     { 
      string pname = this.listBox1.SelectedItem.ToString(); 
      myPrinters.SetDefaultPrinter(pname); 
     } 


     public Form1() 
     { 
      InitializeComponent(); 
      listAllPrinters(); 
     } 
    } 

    public static class myPrinters 
    { 
     [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)] 
     public static extern bool SetDefaultPrinter(string Name); 

    } 
} 
0

bir .NET uygulaması kapsamında Win32API kullanmadan C# .NET ile yapılabilir nasıl. Win32API yaklaşımı, varsayılan yazıcıyı korur Uygulama kapatıldıktan sonra.

using System.Drawing.Printing; 

namespace MyNamespace 
{ 
    public class MyPrintManager 
    { 
    public static PrinterSettings MyPrinterSettings = new PrinterSettings(); 

    public static string Default_PrinterName 
    { 
     get 
     { 
     return MyPrinterSettings.PrinterName; 
     } 
     set 
     { 
     MyPrinterSettings.DefaultPageSettings.PrinterSettings.PrinterName = value; 
     MyPrinterSettings.PrinterName = value; 
     } 
    } 
    } 
}