How to Get the List of All Printers in Computer

How to get the list of all printers in computer

Try this:

foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
{
MessageBox.Show(printer);
}

Batch script to get all printers on computer from all users

Windows stores networked printers under the key HKEY_USERS\{SID}\Printers\Connections\. This is a good start as we can process these for all the users who are logged in. However, when a user logs out, the active user's hive gets closed and saved to NTUSER.DAT under their respective user's directory. i.e. C:\Users\someuser\NTUSER.DAT

So, with logged out users we need to load their NTUSER.DAT file into the registry, read the printers, and then disconnect their NTUSER.DAT file.

Finally, since we can easily grab local printers. The final script should look like so:

@ECHO OFF
CLS
setLocal EnableDelayedExpansion
REM The textfile to store the printers
SET textFile="C:\printers.txt"
REM Clear the text file and start new
COPY /Y NUL !textFile! >nul 2>&1

REM =================================================================================================================
REM Get all networked printers for every user who is currently logged in
REM =================================================================================================================
ECHO ==============================================================
ECHO Processing users who are currently logged in!
ECHO ==============================================================
ECHO.
FOR /F "tokens=*" %%G IN ('REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"') DO (
SET line=%%G
FOR /F "tokens=3" %%X IN ('REG QUERY "HKLM\!line:~19!" /v "profileImagePath" 2^>nul') DO (
SET userPath=%%X
SET userPath=!userPath:*C:\Users\=!

SET isUser=true

REM Specify users to filter out
IF "!userPath!" == "Administrator" SET isUser=false
IF "!userPath!" == "defaultuser0" SET isUser=false
IF "!userPath!" == "Public" SET isUser=false
IF "!isUser!" == "true" (
IF EXIST "C:\users\!userPath!\" (
REM Make sure the key actually exists
REG QUERY "HKU\!line:~76!" >nul 2>&1
IF !ERRORLEVEL! EQU 0 (
ECHO Processing printers for !userPath!
ECHO !userPath!: >> !textFile!
REM Get all network printers
FOR /F "tokens=*" %%F IN ('REG QUERY "HKU\!line:~76!\Printers\Connections" 2^>nul') DO (

REM Format the output to only contain the printer name. Then print it to the text file.
SET newLine=%%F
SET output=!newLine:*Connections\=!
ECHO !output:,=\! >> !textFile!
)
ECHO.>>!textFile!
)
)
)
)
)
ECHO Logged in users are now processed.
ECHO.
REM =================================================================================================================
REM Get all networked printers for users who are logged off
REM =================================================================================================================
ECHO ==============================================================
ECHO Processing users who are logged off.
ECHO ==============================================================
ECHO.
FOR /F "tokens=*" %%D IN ('DIR C:\Users\ /B') DO (
SET line=%%D
SET isUser=true

REM Specify users to filter out
IF "!line!" == "Administrator" SET isUser=false
IF "!line!" == "defaultuser0" SET isUser=false
IF "!line!" == "Public" SET isUser=false
IF "!isUser!" == "true" (
XCOPY "C:\Users\!line!\NTUSER.DAT" "C:\Users\!line!\NTUSER_TEMP.DAT*" /H /Q >nul 2>&1
IF !ERRORLEVEL! EQU 0 (
REG LOAD "HKU\TempHive" "C:\Users\!line!\NTUSER_TEMP.DAT" >nul 2>&1

REM Make sure the key actually exists
REG QUERY "HKU\TempHive\Printers\Connections" >nul 2>&1
IF !ERRORLEVEL! EQU 0 (

REM Get all network printers
ECHO Processing printers for !userPath!
ECHO !line!: >> !textFile!
FOR /F "tokens=*" %%F IN ('REG QUERY "HKU\TempHive\Printers\Connections" 2^>nul') DO (

REM Format the output to only contain the printer name. Then print it to the text file.
SET newLine=%%F
SET output=!newLine:*Connections\=!
ECHO - !output:,=\! >> !textFile!
)
ECHO.>>!textFile!
)

REG UNLOAD "HKU\TempHive" >nul 2>&1
DEL /Q /A:H "C:\Users\!line!\NTUSER_TEMP.DAT"
)
)
)

REM =================================================================================================================
REM Get the locally installed printers
REM =================================================================================================================
ECHO ==============================================================
ECHO Processing locally installed printers
ECHO ==============================================================
ECHO.
ECHO Local Printers:>>!textFile!
FOR /F "tokens=*" %%a in ('WMIC PRINTER GET NAME') do (
SET printer=%%a
IF NOT "!printer:~0,2!" == "\\" (
IF NOT "!printer:~0,4!" == "Name" (
ECHO.!printer! >> !textFile!
)
)
)
ENDLOCAL

NOTE: Make sure to run this script as an administrator so it can load the NTUSER.DAT file into the registry


Additional Note: I answered my own question after I did hours of research into this and not finding an answer online. I decided to create this script and reword my original question so that if anyone else who comes across this might find some use out of it.

How to list all the printer connected on the network using c#?

You can get all connected printers using the GetPrintQueue function in the PrintServer class in the System.Printing namespace. Here is the good sample for this.

Then add them to your list for the drop down.

Also, either do a better search here on stackoverflow (you might find this one or this one), or at least mention what you've got so far and what research you've done.

foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
{
MessageBox.Show(printer);
}


Related Topics



Leave a reply



Submit