c#中如何获取默认打印机的句柄 5
展开全部
首先你要确定打印机在任务管理器中的进程名. 好比是"print.exe"
/// <summary>
/// 根据进程名获取句柄
/// </summary>
/// <param name="exe">进程名</param>
/// <returns>(Intptr)句柄</returns>
private static IntPtr CloseMethod(string exe)
{
if (exe.Substring(exe.Length - 4) == ".exe")
exe = exe.Substring(0, exe.Length - 4);//去掉.exe
IntPtr ihand = new IntPtr();
System.Diagnostics.Process[] myPs;
myPs = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process p in myPs)
{
if (p.ProcessName.ToLower() == exe.ToLower())
{
try
{
ihand = p.Handle;//句柄
}
catch (Exception eKillProcess)
{
}
}
}
return ihand;
}
调用:
MessageBox.Show(CloseMethod("print.exe").ToString());
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询