I am trying to write an application to print PDFs.
Now I get an error message where I can't get any further after a long search.
The goal would be to print a PDF file without opening the PDF-Reader.
My Code:
using System.Diagnostics;
private void button1_Click(object sender, EventArgs e)
{
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = #"C:\testFile.pdf";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.StartInfo = info;
p.Start();
p.WaitForInputIdle();
System.Threading.Thread.Sleep(3000);
if (false == p.CloseMainWindow())
p.Kill();
}
The error condition occurs at p.Start(); line.
Error message:
System.ComponentModel.Win32Exception: "The system cannot find the
specified file".
New experience: 20.01.2021
If I comment out info.Verb = "print";. Then the PDF is opened.
Is this a sign that it finds the PDF but has no access to printer?
use this code it works for me :
public static void PrintToASpecificPirnter()
{
using (PrintDialog printDialog = new PrintDialog())
{
if (printDialog.ShowDialog() == DialogResult.OK)
{
var StartInfo = new ProcessStartInfo();
StartInfo.CreateNoWindow = true;
StartInfo.UseShellExecute = true;
StartInfo.Verb = "printTo";
StartInfo.Arguments = "\"" + printDialog.PrinterSettings.PrinterName + "\"";
StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
StartInfo.FileName = fileName;
Process.Start(StartInfo);
}
}
}
Related
I am having an issuing trying to get a form app on visual studio 19 to execute a cmd on command line for converting a video from mp4 to avi.
I am using ffmpeg for this but everytime I compile it wont pick anything up.
I have ran the argument through command line and it converts the video just fine. The path as far as I am aware is correct so I am not sure why the compiler wont pick up on any files.
private void Button1_Click(object sender, EventArgs e)
{
string cmdString = "c:\ffmpeg\bin";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "ffmpeg.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = cmdString + $"-i shatner.mp4 shatner.avi";
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
}
}
The error I am getting:
"The system cannot find the specified file"
Also I would have put a try catch block around the Process.Start but it doesnt matter since it is still throwing the exception.
Your fileName and arguments are specified incorrectly. Please see below.
private void button1_Click(object sender, EventArgs e)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "c:\\ffmpeg\\bin\\ffmpeg.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "-i shatner.mp4 shatner.avi";
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
using (Process exeProcess = Process.Start(startInfo))
{
string error = exeProcess.StandardError.ReadToEnd();
string output = exeProcess.StandardError.ReadToEnd();
exeProcess.WaitForExit();
MessageBox.Show("ERROR:" + error);
MessageBox.Show("OUTPUT:" + error);
}
}
How can I print PDF file without changing the page size? I use the code below but it changes the paper size.
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = FilePath;
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.StartInfo = info;
p.Start();
p.WaitForInputIdle();
System.Threading.Thread.Sleep(3000);
if (false == p.CloseMainWindow())
p.Kill();
I am trying to print a pdf in a c# console application, by starting a process.
Instead of printing only 1 copy of the document it prints multiple copies (3, 4, 5 or 6 unpredictable). This is my code...
var p = new Process
{
StartInfo = new ProcessStartInfo
{
CreateNoWindow = true,
Verb = "PrintTo",
Arguments = printerName,
FileName = filePath
}
};
p.Start();
Please can you tell me what I'm doing wrong?
You can do that with this code.
private void SendToPrinter(string prepDok)
{
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = prepDok;
info.CreateNoWindow = false;
info.WindowStyle = ProcessWindowStyle.Hidden;
System.Windows.Forms.PrintDialog pd = new System.Windows.Forms.PrintDialog();
pd.PrinterSettings.Copies = 1;
Process p = new Process();
p.StartInfo = info;
p.StartInfo.Arguments = pd.PrinterSettings.PrinterName;
p.Start();
p.CloseMainWindow();
Thread.Sleep(4000);
if (!p.WaitForExit(5000))
{
if (!p.HasExited)
{
p.Kill();
}
}
}
This will set the number of copies to one and print dialog won't be shown.
I dont know where is my missing. Even it dit not write out log. Could you fix my test code:
private void button1_Click(object sender, EventArgs e)
{
try
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe";
startInfo.Arguments = #"/C Import-Module AppLocker;Set-AppLockerPolicy -XMLPolicy 'iPolicy.xml' > C:\log.txt";
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
//startInfo.RedirectStandardOutput = true;
//startInfo.RedirectStandardError = true;
//startInfo.UseShellExecute = false;
//startInfo.CreateNoWindow = false;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
}
catch (Exception ex) {
Debug.Print("Error: " + ex.Message);
}
}
Finally got it to work, code should look like this:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"cmd.exe";
startInfo.Arguments = #"/C powershell.exe /C Import-Module AppLocker;Set-AppLockerPolicy -XMLPolicy 'iPolicy.xml' > C:\log.txt";
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
This worked for me! You can use full paths to cmd.exe and powershell.exe if you feel necessary, it's not that important here.
I am currently trying to disconnect from a network folder through the command line and am using the following code:
System.Diagnostics.Process process2 = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C NET USE F: /delete";
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
process2.StartInfo = startInfo;
process2.Start();
StreamWriter sw = process2.StandardInput;
sw.WriteLine("Y");
sw.Close();
process2.WaitForExit();
process2.Close();
Occasionally, I get the message "Is it ok to continue disconnecting and force them closed? (Y/N) [N]", to which I want to reply "Y", but I seem to be having issues with that working.
Does anyone know why my code is not inputting "Y" to standard input?
Use below code to get the message "Is it ok to continue disconnecting and force them closed? (Y/N) [N]", to which reply "Y"
static void Main(string[] args)
{
System.Diagnostics.Process process2 = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C NET USE F: /delete";
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
process2.StartInfo = startInfo;
process2.Start();
Read(process2.StandardOutput);
Read(process2.StandardError);
while (true)
process2.StandardInput.WriteLine("Y");
}
private static void Read(StreamReader reader)
{
new Thread(() =>
{
while (true)
{
int current;
while ((current = reader.Read()) >= 0)
Console.Write((char)current);
}
}).Start();
}
I think this may help you..