Tehila
Tehila

Reputation: 61

How to execute cmd of print command using C#?

I want to run in C# the command:

PRINT /D:\\rshprt04\p-RSH108 C:\Users\o-tsoudry\Files\Tehila.txt

I tried the next code:

string result = proc.StandardOutput.ReadToEnd();

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
string exception;

try
{
    startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    startInfo.FileName = "cmd.exe";
    string fileName = @"C:\Users\o-tsoudry\Files\Tehila.txt";
    string printer_Name = @"\p-RSH108 ";
    startInfo.Arguments = "PRINT /D:\\rshprt04" + printer_Name + fileName;
    process.StartInfo = startInfo;
    process.Start();
}
catch (Exception ex)
{
    exception = ex.Message;
}

But it's not working.
When I stand on the process in debugging I see the error:

ExitCode = 'proc.ExitCode' threw an exception of type 'System.InvalidOperationException'

Any idea?

Upvotes: 0

Views: 861

Answers (1)

Bhushan Muttha
Bhushan Muttha

Reputation: 430

Try Below :

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = fileName;
psi.Arguments = "\"" + PrinterName + "\"";
psi.Verb = "PrintTo";
Process.Start(psi).WaitForExit();

Upvotes: 2

Related Questions