By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,364 Members | 1,272 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,364 IT Pros & Developers. It's quick & easy.

executing DOS command on client machine using c#.net

P: 17
I am trying to build a web application which can execute dos print command on the client machine.
This application will read a file stored in server and
it will create a folder on client machine and store the file there for future reference.it will also print the contents on the printer attached on client machine. I M using dos command because other options requires manual file>print options to print the file and number of files is too large.
I Want the user to just click on the print button and all the files will be saved and printed.

First thing I would like to ask is , whatever code I will write for execution of dos command(for creation of folder on client machine and printing)will run on server or client machine.as far as I know c# code will execute on server side. What should I do to make it run on client machine.


I have a asp button on the page which when clicked should
create a folder on the clients hard disk eg: c: using dos command
Expand|Select|Wrap|Line Numbers
  1.     MD somefoldername
  2.  
Save the file there and then print the file using dos command
Expand|Select|Wrap|Line Numbers
  1.  PRINT  filename
  2.  
I'm having the following code:
Expand|Select|Wrap|Line Numbers
  1.  System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
  2.         psi.UseShellExecute = false;
  3.         psi.ErrorDialog = true;
  4.         psi.RedirectStandardOutput = true;
  5.         psi.RedirectStandardInput = true;
  6.         psi.RedirectStandardError = true;
  7.         psi.Arguments = "PRINT c:\\test.txt";
  8. //test.txt is already present on clients c:
  9.         System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
  10.  
  11.  
  12.         System.IO.StreamWriter sIn = proc.StandardInput; // Attach the in for writing 
  13.         System.IO.StreamReader sOut = proc.StandardOutput; // Attach the output for reading
  14.         System.IO.StreamReader sErr = proc.StandardError; // Attach the output for errors
  15.  
  16.         proc.Close();
  17.         string results = "";
  18.         results = "<br /><b>Output : </b><br />" + sOut.ReadToEnd().Trim();
  19.         results = "<br /><b>Error : </b><br />" + sErr.ReadLine().ToString();
  20.  
  21.         // Close the io Streams;
  22.         sIn.Close();
  23.         sOut.Close();
  24.         sErr.Close();
  25.         Response.Write(results);
  26.  
But it never executes as it throws win32 exeception..

I Also tried one other piece of code
Expand|Select|Wrap|Line Numbers
  1.                Process proc = new Process();
  2.                proc.StartInfo.UseShellExecute = true;
  3.               proc.StartInfo.FileName = "calc";
  4.               proc.StartInfo.Arguments = null;
  5.              proc.Start();
  6.                proc.WaitForExit();
  7.  
In this case there is no exception but it do nothing.this code should open the windows calculator.
I have searched the google and everywhere I get the same piece of code never mentioning it will execute on server or client. I tried to execute it on server to but with the same result. I will be grateful for the solution. It's very urgent.
Nov 22 '07 #1
Share this Question
Share on Google+
5 Replies


P: 17
anybody plz its very urgent
Nov 22 '07 #2

Frinavale
Expert Mod 5K+
P: 9,731
anybody plz its very urgent
Hi RohitKumar,

Your post is entirely in Caps. Please read the posting guidelines and pay close attention to the section on Things that are generally unacceptable.

-MODERATOR
Nov 22 '07 #3

Frinavale
Expert Mod 5K+
P: 9,731
I Am Trying to build a web application which can execute DOS Print command on the client machine.
What type of application are you building? An ASP.NET Web application or a desktop application that accesses a Web Service?
If you are developing an ASP.NET Web application what you are trying to do is simply not possible without using an ActiveX control. ActiveX controls are considered to be unsafe.

this application will read a file stored in the server and will create a folder on the client machine and store the file there for future reference. It will also pring the contents on the printer attached on the client machine

I am using DOS command because other options require manual File->Print options to print the file and a number of files is too large

I want the user just to click on the Print button and all the files will be saved and printed

First thing I would like to ask is, whatever code I will write for execution of DOS command (for creation of folder on client machine and printing) will run on server or client machine. As far as I know C# code will execute on server side.
I need to know what type of web application you are developing?
An ASP.NET Web application?
If you try to run Dos commands in this type of application, it will execute on the server computer Not on the client computer.

What should I do to make it run on the client machine?
If you are developing an ASP.NET application you would have to write an ActiveX to do this...This is very difficult to do and is not considered safe.

Instead I recommend you just create the files on the server and let the user download them into their own directory. Once the user's finished, just delete these resources.

I have an ASP button on the page which when clicked should create a folder on the client's hard disk eg: C: USING DOS COMMAND
Ah Ha!
You are developing an ASP.NET web application!
The code you have provided will execute on the server alone.

I strongly recommend you reconsider your project's requirements as what you are trying to do is not possible through ASP.NET. You'd have to create an ActiveX control that the user would agree to Install on their computer. It's not easy to do this and ActiveX's are strongly advised against...and your users would be limited to using Internet Explorer Only.

Due to security reasons, your web application cannot control anything on the Client's computers. Web applications are designed to serve the information into Web Pages, not onto the client's computer.

Consider letting the user download the files on their own instead of manipulating the client's computer.

Or you could consider developing a Web Service with a desktop application client that calls your functions. This will let you control the client's computer without any problems.
Nov 22 '07 #4

P: 17
What type of application are you building? An ASP.NET Web application or a desktop application that accesses a Web Service?
If you are developing an ASP.NET Web application what you are trying to do is simply not possible without using an ActiveX control. ActiveX controls are considered to be unsafe.



I need to know what type of web application you are developing?
An ASP.NET Web application?
If you try to run Dos commands in this type of application, it will execute on the server computer Not on the client computer.



If you are developing an ASP.NET application you would have to write an ActiveX to do this...This is very difficult to do and is not considered safe.

Instead I recommend you just create the files on the server and let the user download them into their own directory. Once the user's finished, just delete these resources.



Ah Ha!
You are developing an ASP.NET web application!
The code you have provided will execute on the server alone.

I strongly recommend you reconsider your project's requirements as what you are trying to do is not possible through ASP.NET. You'd have to create an ActiveX control that the user would agree to Install on their computer. It's not easy to do this and ActiveX's are strongly advised against...and your users would be limited to using Internet Explorer Only.

Due to security reasons, your web application cannot control anything on the Client's computers. Web applications are designed to serve the information into Web Pages, not onto the client's computer.

Consider letting the user download the files on their own instead of manipulating the client's computer.

Or you could consider developing a Web Service with a desktop application client that calls your functions. This will let you control the client's computer without any problems.
I m very sorry for using caps in my posting, i will be more careful in the future.
yes,Frinavale i think you are right i would like to develop a web service with a desktop application client.can you plz help me regarding the application
Nov 23 '07 #5

Frinavale
Expert Mod 5K+
P: 9,731
I m very sorry for using caps in my posting, i will be more careful in the future.
yes,Frinavale i think you are right i would like to develop a web service with a desktop application client.can you plz help me regarding the application

You should start by looking up how Web Services work and how to Consume these Services in a desktop application then get back to us with any specific questions that you have on the topic.

-Frinny
Nov 23 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.