Print PDF file with printdialog vb.net - vb.net

I'm trying to print a external pdf file with printdialog options but the file is printed with predeterminated printer config
Dim result As DialogResult = PrintDialog1.ShowDialog()
If (result = DialogResult.OK) Then
Dim psi As New ProcessStartInfo
psi.UseShellExecute = True
psi.Verb = "print"
psi.WindowStyle = ProcessWindowStyle.Hidden
psi.Arguments = PrintDialog1.PrinterSettings.PrinterName.ToString()
psi.FileName = "file.pdf"
Process.Start(psi)
End If

You would need to use the "PrintTo" verb rather than the "Print" verb. You would also need to wrap the printer name in quotes if it might have spaces in it. I would suggest making your code a bit more succinct:
Process.Start(New ProcessStartInfo("file.pdf",
$"""{PrintDialog1.PrinterSettings.PrinterName}""") With {.Verb = "printto",
.UseShellExecute = True,
.WindowStyle = ProcessWindowStyle.Hidden})

Related

Print a pdf to a selected printer using iText7

I Need to print a pdf file to a selected printer.
I have tried using the code below, but it opens adobe and prints to the default printer. Does itext 7 have the ability to print directly to a selected printer using the print dialog box.
I am using visual basic.net
Dim proc As Process = New Process
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
proc.StartInfo.Verb = "print"
'Define location of adobe reader/command line
'switches to launch adobe in "print" mode
proc.StartInfo.FileName = "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe"
proc.StartInfo.Arguments = "/p /h "" " & item & ""
proc.StartInfo.UseShellExecute = False
proc.StartInfo.CreateNoWindow = False
proc.Start()
proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal
If (proc.HasExited = False) Then
proc.WaitForExit(10000)
End If
proc.EnableRaisingEvents = True
proc.Close()
killProcess("AcroRd32")

hide cmd in vb.net not working

I'm trying to hide cmd windows with vb.net without success.
Dim oProcess As New Process()
Dim oStartInfo As New ProcessStartInfo("cmd.exe", " /c cscript ""%windir%\system32\slmgr.vbs"" /xpr | findstr ""The machine""")
oStartInfo.WindowStyle = ProcessWindowStyle.Minimized
oStartInfo.WindowStyle = ProcessWindowStyle.Hidden
oStartInfo.UseShellExecute = False
oStartInfo.RedirectStandardOutput = True
oProcess.StartInfo = oStartInfo
oProcess.Start()
Dim sOutput As String
Using oStreamReader As System.IO.StreamReader = oProcess.StandardOutput
sOutput = oStreamReader.ReadToEnd()
End Using
TextBox4.Text = sOutput
any help please what is the mistake in my code ?
You must set the CreateNoWindow property as well.
oStartInfo.CreateNoWindow = True
Also, this is just redundant:
oStartInfo.WindowStyle = ProcessWindowStyle.Minimized '<-- Remove this line.
oStartInfo.WindowStyle = ProcessWindowStyle.Hidden
Setting WindowStyle to minimized won't affect anything since you change it to Hidden right after. When you use the = operator you replace a variable's or property's current value with a new one.

VB.net Launcher Application Error

i've been created launcher application with vb.net, but when the target application began, there was an error "Can not Read Start.ini file".
I tried with this code
Dim psi As New ProcessStartInfo()
With psi
.FileName = sb.ToString
.UseShellExecute = True
End With
Process.Start(psi)
how to fix it ?
Daniel probably has it right. Try setting the WorkingDirectory property based off your FileName:
Dim psi As New ProcessStartInfo()
With psi
.FileName = sb.ToString
.WorkingDirectory = System.IO.Path.GetDirectoryName(.FileName)
.UseShellExecute = True
End With
Process.Start(psi)

Using cmd in VB, using commands and receiving output

I need to know, if you could help me, how to insert commands in vb then they run in cmd and i get the output.
I need to do "net localgroup Administradores a58465 /add" and get the error message if there is one.
Solution: `Dim myProcess As Process = New Process
Dim s As String
myProcess.StartInfo.FileName = "c:\windows\system32\cmd.exe"
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.CreateNoWindow = True
myProcess.StartInfo.RedirectStandardInput = True
myProcess.StartInfo.RedirectStandardOutput = True
myProcess.StartInfo.RedirectStandardError = True
myProcess.Start()
Dim sIn As System.IO.StreamWriter = myProcess.StandardInput
Dim sOut As System.IO.StreamReader = myProcess.StandardOutput
Dim sErr As System.IO.StreamReader = myProcess.StandardError
'sIn.AutoFlush = True
sIn.Write("cls" & System.Environment.NewLine)
sIn.Write("net user" & System.Environment.NewLine)
sIn.Write("exit" & System.Environment.NewLine)
s = sOut.ReadToEnd()
If Not myProcess.HasExited Then
myProcess.Kill()
End If
LB1.Text = s
LB1.Visible = True
sIn.Close()
sOut.Close()
sErr.Close()
myProcess.Close()`
Check out Process.Start. http://msdn.microsoft.com/en-us/library/0w4h05yb(v=vs.110).aspx
Also look for the ProcessStartInfo class, which will give you options on how to kick off an external process.
Console input and output can be made available to your program through ProcessStartInfo.

Run CMD Silently

I'm trying to run CMD silently, but each time I get an error. Could someone please tell me where I'm going wrong?
Dim myProcess As Process
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
myProcess.StartInfo.CreateNoWindow = True
myProcess.StartInfo.FileName = ("cmd.exe" & CmdStr)
myProcess.Start()
CmdStr is already a string to do certain things that I want in the application.
I suppose your cmdStr is a string with parameters for CMD.
If so you need to use the Arguments property of StartInfo.
You get a Null Exception on the myProcess variable because it is never instatiated with new.
You could create a ProcessStartInfo var to use with the static Process.Start method and set the UseShellExecute to False
Dim startInfo As New ProcessStartInfo("CMD.EXE")
startInfo.WindowStyle = ProcessWindowStyle.Hidden
startInfo.CreateNoWindow = True
startInfo.UseShellExecute = False
startInfo.Arguments = CmdStr
Process.Start(startInfo)
or edit your code to add
myProcess = new Process()
before using the var myProcess