473,322 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

how to execute DOS command in c#.net

I have a button in my asp.net page ,when clicked should execute the following DOS command
TYPE SOMEFILENAME.TXT>PRN
i.e. the dos print command.
i have come across some code which i have tried but does not work. i am putting the code below:
Expand|Select|Wrap|Line Numbers
  1.  Process proc = new Process();
  2.         proc.StartInfo.UseShellExecute = false;
  3.         proc.StartInfo.FileName = "cmd";
  4.         proc.StartInfo.Arguments = "c:\\MCX_CONTRACT\\scrip.txt>prn";
  5.         proc.StartInfo.RedirectStandardOutput = true;
  6.         proc.Start();
  7.         proc.WaitForExit();
  8.  
i wanna what is the problem with the above code and if there is any other alternative plz do help me.
Sep 13 '07 #1
4 8989
Plater
7,872 Expert 4TB
There is a lot wrong with that, you never even use the TYPE command.
Try:
Expand|Select|Wrap|Line Numbers
  1.  Process proc = new Process();
  2.         proc.StartInfo.UseShellExecute = true;
  3.         proc.StartInfo.FileName = "type";
  4.         proc.StartInfo.Arguments = "c:\\MCX_CONTRACT\\scrip.txt>prn";
  5.         proc.Start();
  6.         proc.WaitForExit();
  7.  
Make shell execute be true, I think it might be required.
There's no point in redirecting the standard stream there because the ">" is already redirecting it to the PRN device.

Although I'm not sure how well the redirection operaters work with the Process class.

Have you tried the actual dos print command?
PRINT MYFILENAME.TXT

Expand|Select|Wrap|Line Numbers
  1.  Process proc = new Process();
  2.         proc.StartInfo.UseShellExecute = true;
  3.         proc.StartInfo.FileName = "PRINT";
  4.         proc.StartInfo.Arguments = "c:\\MCX_CONTRACT\\scrip.txt";
  5.         proc.Start();
  6.         proc.WaitForExit();
  7.  
??
Sep 13 '07 #2
ewokspy
30
I also have a problem with getting a DOS command to work properly. I am using C#.NET ver. 2003 so stuck with .NET service pack 1.1 But I need to perform a ping to ensure we can reach the domain. So I figured I'd Shell a DOS promp, and ping the domain and read the response from the DOS prompt, but so far I'm having a lot of trouble reading the response. Here is what I have so far:

Expand|Select|Wrap|Line Numbers
  1.             //Get Process Object
  2.             Process Ping = new System.Diagnostics.Process();
  3.  
  4.             //Get Domain from registry
  5.             string myDNSHost = GetSystemDNSHost();
  6.  
  7.             //do a command line Ping
  8.             Ping.EnableRaisingEvents = false;
  9.             Ping.StartInfo.UseShellExecute = true;
  10.             Ping.StartInfo.FileName = "Ping";
  11.             Ping.StartInfo.Arguments = myDNSHost;
  12.             Ping.StartInfo.RedirectStandardOutput = true;
  13.             Ping.Start();
  14.             string myOutput = Ping.StandardOutput.ReadToEnd();
  15.             Ping.WaitForExit();
Thanks for any help.
Sep 18 '07 #3
Plater
7,872 Expert 4TB
A lot of DOS commands output text to StandardError as well (although you can't tell the difference)
So you would need to redirect that as well.

Here's an example I use for doing NSLOOKUP
Expand|Select|Wrap|Line Numbers
  1. private string GetLookup(string IPAddy)
  2.         {
  3.             string retval = "";
  4.             ProcessStartInfo psi = new ProcessStartInfo("nslookup",IPAddy);
  5.             psi.UseShellExecute=false; 
  6.             psi.RedirectStandardOutput = true;
  7.             psi.RedirectStandardInput = true;
  8.             psi.RedirectStandardError  = true;
  9.             psi.CreateNoWindow = true;
  10.             Process p = new Process();
  11.             p.StartInfo=psi;
  12.             p.Start();
  13.             StreamReader se = p.StandardError; 
  14.             StreamReader so = p.StandardOutput;
  15.             retval += so.ReadToEnd();
  16.             retval += se.ReadToEnd();
  17.  
  18.             while (!p.HasExited)
  19.             {
  20.                 retval += so.ReadToEnd();
  21.                 retval += se.ReadToEnd();
  22.             }
  23.             retval += so.ReadToEnd();
  24.             retval += se.ReadToEnd();
  25.             p.Close();
  26.             p.Dispose();
  27.             return retval;
  28.         }
  29.  
Sep 18 '07 #4
ewokspy
30
:D

I knew it had something to do with that property, but I just couldn't get the right order to call them in. Thank you.
Sep 18 '07 #5

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

Similar topics

2
by: michaaal | last post by:
I feel a bit silly asking this because I use this code all the time, but what does the ",,129" mean? Are there other parameters that one might use (I only do fairly simple SQL commands). ...
4
by: Kartik | last post by:
Hi, I have an ASP.NET application using VB.NET.I am sending a DOS command to a machine on the network to print a file. This is achieved using xp_cmdshell Dim str As String = "xp_cmdshell...
3
by: Me | last post by:
hi, I have this file I need to execute from an ASP client. since we have installed it on the SQL Database server machine, I thought I could run it like this: Set Cn =...
1
by: Matt | last post by:
I used to execute a command line script in IIS 4 using Dynuexec. This is a third part server object I installed on the server (http://www.dynu.com/dynuexec.asp). I installed this object onto my...
5
by: Gustavo Randich | last post by:
Hello, I'm writing an automatic SQL parser and translator from Informix to DB2. Now I'm faced with one of the most difficult things to translate, the "foreach execute procedure" functionality...
2
by: Norman Fritag | last post by:
Hi there The below code executes some queries. As newbie I was wondering weather you are better of using connection execute or command execute to execute queries? I am asking as...
4
by: Chris | last post by:
I posted this in the C# language group, then thought it might be more appropriate in this group. I would not cross-post except I want the answer so badly. I built small C# Web and Web Service...
2
by: Wei Wang | last post by:
Hi, I want to do a select in dynamic command, something like: TRIGGER FUNCTION DECLARE table_name_suffix text; temp_result RECORD; temp_result2 RECORD;
4
by: alberto | last post by:
How can I execute a command of the operating system like "cls" from a C# code? Thank you
6
by: Oliver | last post by:
I'm fairly new to DB2. I have been assigned to build a delete trigger that finds the data type of each of the table's fields so that the trigger can then build a string consisting of OLD values...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.