Thread: how to call an exe file in a c# program?

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    183

    how to call an exe file in a c# program?

    Hello,

    I have a program in the form of an exe file which is run through the command prompt.
    I need to call it in the middle of my C# code.
    Can you please tell me how I can call an exe file in a C# code.

    I use visual studio 2008.

    Thanks
    Arian

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    System.Diagnostics.Process.Start("command.exe");
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    183
    Thankyou,

    but I couldn't run the program. I think there is sht wrong with the address

    to run the program I need 3 files and I write the following line in the command prompt window ("tnt" is the name of the exe file).

    Code:
    H:\test\test>tnt file1 file2.tt > file3.tts
    I copied and pasted all the required files in the folder of my c# program (test).

    here is the code:
    Code:
                //string path=""; 
                string path = @"H:\test\test\";
                string corpus =path +  "file1";
                string input = path + "file2.tt";
                string output = path + "file3.tts";
                string prog =path +  "tnt.exe";
    
                string command = prog + " " + corpus + " " + input + " > " + output;
                System.Diagnostics.Process.Start(command);
    I get this error at the last line: "The system cannot find the file specified"

    can you please help me?

    Thank you
    Arian

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    183
    I change the code to
    Code:
                string path = @"H:\test\test\";
                //string path = "";
                string corpus =path +  "file1.tt";
                string input = path + "file2.tt";
                string output = path + "file3.tts";
                string prog = path + "tnt.exe";
    
                string command = prog + " " + corpus + " " + input + " > " + output;
    
    
                Process proc = new Process();
                proc.StartInfo.FileName = prog;
                proc.StartInfo.Arguments = command;
                proc.Start();
    now it runs but there is no output in the file3!!!

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    that's because "> fil3.tts" isn't an argument passed to the command; it's interpreted by the shell to redirect stdout so your tnt program really only sees the file1.tt and file2.tt arguments.

    Check out the proc.StartInfo.RedirectStandardOutput property.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Nov 2003
    Posts
    183
    Using the instruction on
    How to Execute a Command in C# ? - CodeProject
    solved the output problem but I needed to move the exe file and the required source files to the debug folder of the c# program.

    Do you know if it is possible to change the address to another folder or not?

    thank you
    Arian

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Sure, just include it as part of the filename string:
    Code:
    proc.StartInfo.FileName = @"..\debug\tnt.exe";
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Nov 2003
    Posts
    183
    it doesn't work when I add address to the file names

    Code:
                string command = "tnt.exe file1 file2.tt > file3.tts";
    
                System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd","/c " + command);
    
                // The following commands are needed to redirect the standard output.
                // This means that it will be redirected to the Process.StandardOutput StreamReader.
                procStartInfo.RedirectStandardOutput = true;
                procStartInfo.UseShellExecute = false;
                // Do not create the black window.
                procStartInfo.CreateNoWindow = true;
                // Now we create a process, assign its ProcessStartInfo and start it
                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                proc.StartInfo = procStartInfo;
                proc.Start();

  9. #9
    Registered User
    Join Date
    Nov 2003
    Posts
    183
    sorry!
    it works, I had written something wrong.

    Thank you so much for helping

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  3. Launching program from resources
    By rickykoh1983 in forum Windows Programming
    Replies: 26
    Last Post: 09-03-2003, 12:21 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM