PDA

View Full Version : سوال: تغییر set default پرینتر در سی شارپ؟



malihehshajari
سه شنبه 10 مرداد 1391, 18:42 عصر
با عرض سلام :
من یک برنامه نوشتم که داخل اون از 4 پرینتر استفاده کردم و بنا بر نیاز با استفاده از کد set default پرینتر رو تغییر می دم این کار روی یک کامپیوتر جواب می ده اما وقتی من برنامم رو تو شبکه اجرا میکنم و پرینتر ها رو share می کنم روی سیستمی که پرینتر ها share شدن جواب نمیده چون اسم پرینتر ها با اون چیزی که روی سیستم اصلی هست فرق می کنه
کسی میتونه من رو راهنمایی کنه ؟
خیلی بهش نیاز دارم ممنون میشم اگه کسی میدونه بگه .

ali_habibi1384
سه شنبه 10 مرداد 1391, 21:17 عصر
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);

}
}

Esikhoob
سه شنبه 10 مرداد 1391, 21:25 عصر
شاید این کمک کند:
http://www.news2news.com/vfp/?example=146&ver=vcs&PHPSESSID=33c985ea382c607725de85ce1cfe9aeb

malihehshajari
چهارشنبه 11 مرداد 1391, 08:54 صبح
شاید این کمک کند:
http://www.news2news.com/vfp/?example=146&ver=vcs&PHPSESSID=33c985ea382c607725de85ce1cfe9aeb
خیلی ممنون بازم درست نشد . این کد هم مثل کدی که خودم نوشتم عمل میکنه

malihehshajari
چهارشنبه 11 مرداد 1391, 08:54 صبح
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);

}
}


خیلی ممنون بازم درست نشد . این کد هم مثل کدی که خودم نوشتم عمل میکنه
آیا راهی هست که بتونم اسم کامپیوتر رو از طریق کد نویسی بدست بیارم و بر اساس اون نام پرینتر هام رو تعریف کنم ؟

h_assefi
چهارشنبه 11 مرداد 1391, 13:49 عصر
دو مدل دستور زیر برای بدست آوردن اسم کامپیوتره :
1)using System.Security.Principal;

WindowsIdentity.GetCurrent().Name.ToString();

2)
System.Environment.MachineName