how to execute DOS command in c#.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rohitkumar
    New Member
    • Aug 2007
    • 17

    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.TX T>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:
    Code:
     Process proc = new Process();
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.FileName = "cmd";
            proc.StartInfo.Arguments = "c:\\MCX_CONTRACT\\scrip.txt>prn";
            proc.StartInfo.RedirectStandardOutput = true;
            proc.Start();
            proc.WaitForExit();
    i wanna what is the problem with the above code and if there is any other alternative plz do help me.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    There is a lot wrong with that, you never even use the TYPE command.
    Try:
    Code:
     Process proc = new Process();
            proc.StartInfo.UseShellExecute = true;
            proc.StartInfo.FileName = "type";
            proc.StartInfo.Arguments = "c:\\MCX_CONTRACT\\scrip.txt>prn";
            proc.Start();
            proc.WaitForExit();
    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

    Code:
     Process proc = new Process();
            proc.StartInfo.UseShellExecute = true;
            proc.StartInfo.FileName = "PRINT";
            proc.StartInfo.Arguments = "c:\\MCX_CONTRACT\\scrip.txt";
            proc.Start();
            proc.WaitForExit();
    ??

    Comment

    • ewokspy
      New Member
      • Sep 2007
      • 30

      #3
      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:

      Code:
      			//Get Process Object
      			Process Ping = new System.Diagnostics.Process();
      			
      			//Get Domain from registry
      			string myDNSHost = GetSystemDNSHost();
      
      			//do a command line Ping
      			Ping.EnableRaisingEvents = false;
      			Ping.StartInfo.UseShellExecute = true;
      			Ping.StartInfo.FileName = "Ping";
      			Ping.StartInfo.Arguments = myDNSHost;
      			Ping.StartInfo.RedirectStandardOutput = true;
      			Ping.Start();
      			string myOutput = Ping.StandardOutput.ReadToEnd();
      			Ping.WaitForExit();
      Thanks for any help.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        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
        Code:
        private string GetLookup(string IPAddy)
                {
                    string retval = "";
                    ProcessStartInfo psi = new ProcessStartInfo("nslookup",IPAddy);
                    psi.UseShellExecute=false; 
                    psi.RedirectStandardOutput = true;
                    psi.RedirectStandardInput = true;
                    psi.RedirectStandardError  = true;
                    psi.CreateNoWindow = true;
                    Process p = new Process();
                    p.StartInfo=psi;
                    p.Start();
                    StreamReader se = p.StandardError; 
                    StreamReader so = p.StandardOutput;
                    retval += so.ReadToEnd();
                    retval += se.ReadToEnd();
                    
                    while (!p.HasExited)
                    {
                        retval += so.ReadToEnd();
                        retval += se.ReadToEnd();
                    }
                    retval += so.ReadToEnd();
                    retval += se.ReadToEnd();
                    p.Close();
                    p.Dispose();
                    return retval;
                }

        Comment

        • ewokspy
          New Member
          • Sep 2007
          • 30

          #5
          :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.

          Comment

          Working...