c#中如何获取默认打印机的句柄

2025-01-01 12:22:58
推荐回答(1个)
回答1:

首先你要确定打印机在任务管理器中的进程名. 好比是"print.exe"

 /// 
        /// 根据进程名获取句柄
        /// 

        /// 进程名
        /// (Intptr)句柄
        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());