在我的工作中,我们按用户安装打印机。我正在尝试编写一个备份程序,它会列出给定用户名的所有联网打印机。那可能吗?通过WMI或System.IO就可以了。下面列出了所有联网打印机但不是每个用户的代码。
private void Button_Click(object sender, EventArgs e)
{
ConnectionOptions objConnection = new ConnectionOptions();
objConnection.Impersonation = ImpersonationLevel.Impersonate;
objConnection.EnablePrivileges = true;
string backupselectedcomputer = "Some Computer Name"
ManagementScope objScope = new ManagementScope("\\\\" + backupselectedcomputer +"\\root\\cimv2",objConnection);
objScope.Connect();
SelectQuery selectQuery = new SelectQuery();
selectQuery.QueryString = "Select * from win32_Printer Where Local = FALSE";
ManagementObjectSearcher MOS = new ManagementObjectSearcher(objScope, selectQuery);
ManagementObjectCollection MOC = MOS.Get();
foreach (ManagementObject mo in MOC)
{
//lbBackupprinters is a list box
lbBackupprinters.Items.Add(mo["Name"].ToString().ToUpper());
}
}
答案 0 :(得分:1)
想出办法。以下是遇到此问题的任何人的代码。
private void ddlBackupselectuser_SelectionChangeCommitted(object sender, EventArgs e)
{
lbBackupprinters.Items.Clear();
string selecteduser = ddlBackupselectuser.Text;
string computer = ddlBackupselectcomp.Text;
string sid;
lblBackuppwd.Visible = true;
txtBackuppwd.Visible = true;
cboBackuppwdshow.Visible = true;
//BEGIN GRAB PRINTERS FROM REGISTRY
try
{
NTAccount ntuser = new NTAccount(selecteduser);
SecurityIdentifier sID = (SecurityIdentifier)ntuser.Translate(typeof(SecurityIdentifier));
lblBackupStatus.Text = sID.ToString();
sid = sID.ToString();
}
catch (IdentityNotMappedException)
{
lblBackupStatus.Text = "ERROR "+ ddlBackupselectuser.Text.ToString() + " contains no profile information";
lbBackupprinters.Items.Add("No Printers Found");
return;
}
ConnectionOptions co = new ConnectionOptions();
co.EnablePrivileges = true;
co.Impersonation = ImpersonationLevel.Impersonate;
System.Net.IPHostEntry h = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
string IPAddress = h.AddressList.GetValue(0).ToString();
string lm = System.Net.Dns.GetHostName().ToString();
try
{
ManagementScope myScope = new ManagementScope("\\\\" + computer + "\\root\\default", co);
ManagementPath mypath = new ManagementPath("StdRegProv");
ManagementClass wmiRegistry = new ManagementClass(myScope, mypath, null);
const uint HKEY_LOCAL_MACHINE = unchecked((uint)0x80000002);
//ManagementClass wmiRegistry = new ManagementClass("root/default",
//"StdRegProv",null);
string keyPath = @"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Print\\Providers\\Client Side Rendering Print Provider\\" + sid + "\\Printers\\Connections";
object[] methodArgs = new object[] { HKEY_LOCAL_MACHINE, keyPath, null };
uint returnValue = (uint)wmiRegistry.InvokeMethod("EnumKey", methodArgs);
if (null != methodArgs[2])
{
string[] subKeys = methodArgs[2] as String[];
if (subKeys == null)
{
lbBackupprinters.Items.Add("No Printers Found");
return;
}
ManagementBaseObject inParam = wmiRegistry.GetMethodParameters("GetStringValue");
inParam["hDefKey"] = HKEY_LOCAL_MACHINE;
string keyName = "";
foreach (string subKey in subKeys)
{
//Display application name
keyPath = @"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Print\\Providers\\Client Side Rendering Print Provider\\" + sid + "\\Printers\\Connections" + subKey;
keyName = "DisplayName";
inParam["sSubKeyName"] = keyPath;
inParam["sValueName"] = keyName;
ManagementBaseObject outParam = wmiRegistry.InvokeMethod("GetStringValue", inParam, null);
lbBackupprinters.Items.Add(subKey);
}
}
else
{
lbBackupprinters.Items.Add("No Printers Found");
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}