Printing problem using - System.Diagnostics.ProcessStartInfostartInfo

I

Iain Wilson

Hi All

Using IIS 6.0 - Windows 2003 Server
ASP.Net 2.0 Framework
C#

Using Impersonation with Network Administrator username/Password


I am attempting to print an Adobe PDF document from a web page

using the code listed at the bottom of this message.

When I press the button to print I get the following output

Both the Adobe Exe and the PDF documents have been copied to the
Namespace on the IIS server.
Tried using @"C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe";
but that does not work either

First am I logged in as the Administrator on the IIS server and
therfore should have the same permissions as the network
adimistrator !!!

Secondly, if not how can I tell who I am logged on as ?

From what I have read I think that the code should work but I cannot
work out why it will not do thge job

Any assistance offered will be greatfully accepted

Best regards

Iain

++++++++++++++++++++++++++++++++++++++++++++
Application Path Is D:\inetpub_dmc_test\wwwroot\TestPrintExample\


File Name = D:\inetpub_dmc_test\wwwroot\TestPrintExample\Web Order
CE41584650 For UPS.pdf
Printer Name = Finance Canon
Acrobat Application Path = D:\inetpub_dmc_test\wwwroot\TestPrintExample
\AcroRd32.exe
Process Created
Applied Arguments : /h /t "D:\inetpub_dmc_test\wwwroot\TestPrintExample
\Web Order CE41584650 For UPS.pdf" "Finance Canon"


Full Exception Details : Error Code = -2147467259
Base Exception Is System.ComponentModel.Win32Exception: Access is
denied at
System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo
startInfo) at System.Diagnostics.Process.Start() at
System.Diagnostics.Process.Start(ProcessStartInfo startInfo) at
TestPrintExample.WebForm1.PrintDoc(String PFileName)
Inner Exception Is


Permission Error : Access is denied



Process Complete
++++++++++++++++++++++++++++++++++++++++++++


The code I am using is

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (! IsPostBack)
{
string pkInstalledPrinters;
for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
{
pkInstalledPrinters = PrinterSettings.InstalledPrinters;
ddlPrinterList.Items.Add(pkInstalledPrinters);
}
}
}


public void PrintDoc(string PFileName)
{
string AppPath = "";
label1.Text = " ";

string TheAppPath =
HttpContext.Current.Request.PhysicalApplicationPath;
string pdfFileName = TheAppPath + PFileName;

label1.Text = label1.Text + "<br> Application Path Is " +
TheAppPath + "<br> <br>";
label1.Text = label1.Text + "<br>" + "File Name = " +
pdfFileName;

// string printerName = @"\\Dun-Server3\Finance Canon";
string printerName = ddlPrinterList.SelectedItem.Text;
label1.Text = label1.Text + "<br>" + "Printer Name = " +
printerName;

if (TheApp(PFileName) != "UNKNOWN")
{
if (TheApp(PFileName) == "WINWORD")
{
AppPath = @"C:\Program Files\Microsoft Office
\OFFICE11\WINWORD.EXE";
label1.Text = label1.Text + "<br>" + "Word Application Path
= " + AppPath;
}
if (TheApp(PFileName) == "AcroRd32")
{
// AppPath = @"C:\Program Files\Adobe\Acrobat 7.0\Reader
\AcroRd32.exe";
AppPath = TheAppPath + "AcroRd32.exe";
label1.Text = label1.Text + "<br>" + "Acrobat Application
Path = " + AppPath;
}

System.Diagnostics.ProcessStartInfo startInfo = new
ProcessStartInfo();
label1.Text = label1.Text + "<br>" + "Process Created";


// Print PDF file using Acrobat Reader 6.0.
// "/h" - hidden mode
// "/t" - print command following by the file name and printer
name
// "/h /t \"" + pdfFileName + "\" \"" + printerName + "\"";
startInfo.Arguments = "/h /t \"" + pdfFileName + "\" \"" +
printerName + "\"";
label1.Text = label1.Text + "<br>" + "Applied Arguments : " +
startInfo.Arguments;

startInfo.CreateNoWindow = false;
startInfo.WindowStyle = ProcessWindowStyle.Normal; // CHange
To Hidden

startInfo.FileName = AppPath;
startInfo.UseShellExecute = true;
startInfo.Verb = "Print";

if(impersonateValidUser("Administrator", "MyDomain",
"AdminPassword"))
{
try
{
System.Diagnostics.Process process =
Process.Start(startInfo);
process.WaitForExit(30000);
if (process.HasExited == false)
{
label1.Text = label1.Text + "<br><br><br>" + "Process
Has Not Exited : Kill Process" + "<br><br><br>";
process.Kill();
}
}
catch (Win32Exception ex)
{
label1.Text = label1.Text + "<br><br><br>" + "Full
Exception Details : Error Code = " +
ex.ErrorCode + "<br>" + "Base Exception Is
" +
ex.GetBaseException() + "<br>" + "Inner
Exception Is " +
ex.InnerException;
if(ex.NativeErrorCode == ERROR_FILE_NOT_FOUND)
{
label1.Text = label1.Text + "<br><br><br>" + "Path
Error : " + ex.Message + "<br><br><br>";
}
else
if (ex.NativeErrorCode == ERROR_ACCESS_DENIED)
{
// Note that if your word processor might generate
exceptions
// such as this, which are handled first.
label1.Text = label1.Text + "<br><br><br>" +
"Permission Error : " + ex.Message + "<br><br><br>";
}
else
{
label1.Text = label1.Text + "<br><br><br>" +
"Permission Error : " + ex.Message + "<br><br><br>";
}
}
}
label1.Text = label1.Text + "<br>" + "Process Complete";
}
}



private void button2_Click(object sender, System.EventArgs e)
{
string LFileName = @"Web Order CE41584650 For UPS.pdf";
PrintDoc(LFileName);
}
 
F

Family Tree Mike

Iain,

It looks like you are trying to specify a full command line to print. Look
at this link
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx. The
examples are to print a word document. The filename should be the file to
print, and the verb should be "Print". The operating system will handle
finding the correct application for the verb Print with the filename given.


Iain Wilson said:
Hi All

Using IIS 6.0 - Windows 2003 Server
ASP.Net 2.0 Framework
C#

Using Impersonation with Network Administrator username/Password


I am attempting to print an Adobe PDF document from a web page

using the code listed at the bottom of this message.

When I press the button to print I get the following output

Both the Adobe Exe and the PDF documents have been copied to the
Namespace on the IIS server.
Tried using @"C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe";
but that does not work either

First am I logged in as the Administrator on the IIS server and
therfore should have the same permissions as the network
adimistrator !!!

Secondly, if not how can I tell who I am logged on as ?

From what I have read I think that the code should work but I cannot
work out why it will not do thge job

Any assistance offered will be greatfully accepted

Best regards

Iain

++++++++++++++++++++++++++++++++++++++++++++
Application Path Is D:\inetpub_dmc_test\wwwroot\TestPrintExample\


File Name = D:\inetpub_dmc_test\wwwroot\TestPrintExample\Web Order
CE41584650 For UPS.pdf
Printer Name = Finance Canon
Acrobat Application Path = D:\inetpub_dmc_test\wwwroot\TestPrintExample
\AcroRd32.exe
Process Created
Applied Arguments : /h /t "D:\inetpub_dmc_test\wwwroot\TestPrintExample
\Web Order CE41584650 For UPS.pdf" "Finance Canon"


Full Exception Details : Error Code = -2147467259
Base Exception Is System.ComponentModel.Win32Exception: Access is
denied at
System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo
startInfo) at System.Diagnostics.Process.Start() at
System.Diagnostics.Process.Start(ProcessStartInfo startInfo) at
TestPrintExample.WebForm1.PrintDoc(String PFileName)
Inner Exception Is


Permission Error : Access is denied



Process Complete
++++++++++++++++++++++++++++++++++++++++++++


The code I am using is

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (! IsPostBack)
{
string pkInstalledPrinters;
for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
{
pkInstalledPrinters = PrinterSettings.InstalledPrinters;
ddlPrinterList.Items.Add(pkInstalledPrinters);
}
}
}


public void PrintDoc(string PFileName)
{
string AppPath = "";
label1.Text = " ";

string TheAppPath =
HttpContext.Current.Request.PhysicalApplicationPath;
string pdfFileName = TheAppPath + PFileName;

label1.Text = label1.Text + "<br> Application Path Is " +
TheAppPath + "<br> <br>";
label1.Text = label1.Text + "<br>" + "File Name = " +
pdfFileName;

// string printerName = @"\\Dun-Server3\Finance Canon";
string printerName = ddlPrinterList.SelectedItem.Text;
label1.Text = label1.Text + "<br>" + "Printer Name = " +
printerName;

if (TheApp(PFileName) != "UNKNOWN")
{
if (TheApp(PFileName) == "WINWORD")
{
AppPath = @"C:\Program Files\Microsoft Office
\OFFICE11\WINWORD.EXE";
label1.Text = label1.Text + "<br>" + "Word Application Path
= " + AppPath;
}
if (TheApp(PFileName) == "AcroRd32")
{
// AppPath = @"C:\Program Files\Adobe\Acrobat 7.0\Reader
\AcroRd32.exe";
AppPath = TheAppPath + "AcroRd32.exe";
label1.Text = label1.Text + "<br>" + "Acrobat Application
Path = " + AppPath;
}

System.Diagnostics.ProcessStartInfo startInfo = new
ProcessStartInfo();
label1.Text = label1.Text + "<br>" + "Process Created";


// Print PDF file using Acrobat Reader 6.0.
// "/h" - hidden mode
// "/t" - print command following by the file name and printer
name
// "/h /t \"" + pdfFileName + "\" \"" + printerName + "\"";
startInfo.Arguments = "/h /t \"" + pdfFileName + "\" \"" +
printerName + "\"";
label1.Text = label1.Text + "<br>" + "Applied Arguments : " +
startInfo.Arguments;

startInfo.CreateNoWindow = false;
startInfo.WindowStyle = ProcessWindowStyle.Normal; // CHange
To Hidden

startInfo.FileName = AppPath;
startInfo.UseShellExecute = true;
startInfo.Verb = "Print";

if(impersonateValidUser("Administrator", "MyDomain",
"AdminPassword"))
{
try
{
System.Diagnostics.Process process =
Process.Start(startInfo);
process.WaitForExit(30000);
if (process.HasExited == false)
{
label1.Text = label1.Text + "<br><br><br>" + "Process
Has Not Exited : Kill Process" + "<br><br><br>";
process.Kill();
}
}
catch (Win32Exception ex)
{
label1.Text = label1.Text + "<br><br><br>" + "Full
Exception Details : Error Code = " +
ex.ErrorCode + "<br>" + "Base Exception Is
" +
ex.GetBaseException() + "<br>" + "Inner
Exception Is " +
ex.InnerException;
if(ex.NativeErrorCode == ERROR_FILE_NOT_FOUND)
{
label1.Text = label1.Text + "<br><br><br>" + "Path
Error : " + ex.Message + "<br><br><br>";
}
else
if (ex.NativeErrorCode == ERROR_ACCESS_DENIED)
{
// Note that if your word processor might generate
exceptions
// such as this, which are handled first.
label1.Text = label1.Text + "<br><br><br>" +
"Permission Error : " + ex.Message + "<br><br><br>";
}
else
{
label1.Text = label1.Text + "<br><br><br>" +
"Permission Error : " + ex.Message + "<br><br><br>";
}
}
}
label1.Text = label1.Text + "<br>" + "Process Complete";
}
}



private void button2_Click(object sender, System.EventArgs e)
{
string LFileName = @"Web Order CE41584650 For UPS.pdf";
PrintDoc(LFileName);
}
 
I

Iain Wilson

Hi Mike

Sorry to be a pain in the rear end but thanks for that.
Changed my code as per the link to the code at the bottom of this
post.

This works perfectly well with pdf documents. However when attempting
to print Word or Excel documents the exception thrown is

Full Exception Details : Error Code = -2147467259
Base Exception Is System.ComponentModel.Win32Exception: The system
cannot find the file specified at
System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo
startInfo) at System.Diagnostics.Process.Start() at
TestPrintExample.WebForm1.PrintDoc(String PFileName) in c:\Inetpub
\wwwroot\TestPrintExample\WebForm1.aspx.cs:line 130

The 3 files I am trying to print are all in the application directory.
I can see Adobe, WINWORD, EXCEL start up in the task list but I get no
print.

I then have to manually kill the process.

Best regards

Iain


---------------------------------------------------------------------------------------------


public void PrintDoc(string PFileName)
{

string TheAppPath =
HttpContext.Current.Request.PhysicalApplicationPath;
string TheFileName = TheAppPath + PFileName;

Process myProcess = new Process();
try
{
label1.Text = label1.Text + "<br><br>Entered The Print
Process<br>Printing Document : " + TheFileName;
myProcess.StartInfo.FileName = TheFileName;
myProcess.StartInfo.Verb = "Print";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
LHasRun = true;
label1.Text = label1.Text + "<br>" + "PDF Printing Complete";
myProcess.WaitForExit(60000);
}
catch (Win32Exception ex)
{
label1.Text = label1.Text + "<br><br><br>" + "Full Exception
Details : Error Code = " +
ex.ErrorCode + "<br>" + "Base Exception Is " +
ex.GetBaseException() + "<br>" + "Inner Exception Is " +
ex.InnerException + "<br><br><br>";

if(ex.NativeErrorCode == ERROR_FILE_NOT_FOUND)
{
label1.Text = label1.Text + "<br><br><br>" +
"Path Error : " + ex.Message +
"<br><br><br>";
}
else
if (ex.NativeErrorCode == ERROR_ACCESS_DENIED)
{
label1.Text = label1.Text + "<br><br><br>" +
"Permission Error : " + ex.Message +
"<br><br><br>";
}
else
{
label1.Text = label1.Text + "<br><br><br>" +
"Unknown Error : " + ex.Message +
"<br><br><br>";
}
}
}

private void button2_Click(object sender, System.EventArgs e)
{
string LFileName = "Web Order CE41584650 For UPS.pdf";
PrintDoc(LFileName);
}

private void button1_Click(object sender, System.EventArgs e)
{ // Excel Print Routine For
string LFileName = "Telephone List.xls";
PrintDoc(LFileName);
}

private void button3_Click(object sender, System.EventArgs e)
{ // Word Print Routine
string LFileName = "In addition to these devices.doc";
PrintDoc(LFileName);
}
 
F

Family Tree Mike

I'm guessing that the file name is not completely correct. I have had no
luck unless using Server.MapPath("Filename.txt"). This gets the full system
path to the file rather than the relative path off of wwwroot/appname. You
would then pass this into the process filename property. Hopefully this gets
you further.
 
I

Iain Wilson

Hi Mike

I have changed the code using Server.MapPath but to no avail.
Prints the Adobe PDF document to the default local printer
(I am running this localhost). I Have not run it on the server yet.

What confuses me is

1. All the documents to be printed are in the same directory -
The application directory - so the authorities to the objects
should all be the same. All objects have the same Access
properties.If it can find the PDF document then why can't
it find the .doc and .xls documents.
2. Each of the applications starts up. Adobe, Excel, Word.
I can see the application start in the task manager.
This suggests that both Excel and Word are trying to
perform the required action but failing;
3. The catch throws an Access Denied exception in response
to the error.
(The catch does not throw an exception if there is an existing
instance of the Word or Excel application running. The page
just hangs and no exception is thrown).
I have a small piece of code to forceably kill the process if
it does not end itself. In both the Excel and WORK instances
the code throws an exception

No process is associated with this object

try
{
if (myProcess.HasExited == false)
{
myProcess.Kill();
}
}
catch (Win32Exception ex)
{
label1.Text = label1.Text + "<br><br><br>" + "Cannot Kill Process :
Error Code = " +
ex.ErrorCode + "<br>" + "Base Exception Is " +
ex.GetBaseException() + "<br>" + "Inner Exception Is " +
ex.InnerException + "<br><br><br>";
}


Surely if the process has started then there must be a process
associated with the process object !!!!

I am confused.

best regards

Iain


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Code
-------

try
{
myProcess.StartInfo.FileName = Server.MapPath (PFileName);
myProcess.StartInfo.Verb = "Print";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
myProcess.WaitForExit(20000);
}
catch (Win32Exception ex)
{
if(ex.NativeErrorCode == ERROR_FILE_NOT_FOUND)
{
label1.Text = label1.Text + "<br><br><br>" + "Path Error : " +
ex.Message + "<br><br><br>";
}
else
if (ex.NativeErrorCode == ERROR_ACCESS_DENIED)
{
label1.Text = label1.Text + "<br><br><br>" + "Permission Error : "
+ ex.Message + "<br><br><br>";
}
else
{
label1.Text = label1.Text + "<br><br><br>" + "Unknown Error : " +
ex.Message + "<br><br><br>";
}
}
 
F

Family Tree Mike

Outside of the web application with the word and xls files where the website
tries to open them, can you successfully open them from windows explorer?
Also try printing from explorer. I'm hoping this will provide a clue to what
is going on with office files.
 
I

Iain Wilson

From Windows explorer I can

Open both the Excel and Word documents (All looks well) and Right
click and print both documents.

When I right click and select print

The appropriate application opens up
The document is sent to the printer
The application closes down
The document is printed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top