Executing a .exe-program with arguments

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Saser
    New Member
    • Jul 2009
    • 17

    Executing a .exe-program with arguments

    First post, lol.

    Please bear with my English, I'm a 14 year old guy from Sweden.

    I just started learning C# a few days ago, so I wanted to create something simple (but yet still useful).

    I'm making a console application which launches the compilers for different programming languages. So far have I got compilers for Java, C/C++, C# and Pascal. This is how the program works:
    1. The user types in the path to the file (s)he wants to compile.
    2. The user selects which language the program is written in using numbers.
    3. The user decides whether (s)he wants to compile the file or not. This is only to confirm the compiling, so everything doesn't happen too fast, if you understand what I mean.
    4. When the user have typed "yes" and pressed Enter, the program uses a void called Compile() to launch the right compiler for each language.
    ^ This is where the problem appears.

    I work using Visual C# 2008 Express Edition. When I save my project, Visual C# automatically generates a folder called whatever the project is called (in this case, FayconCMD). It generates a folder called Release, where the final program is (FayconCMD.exe) .
    I've copied the C# compiler from .NET v3.5 into this folder. In the program, the "path" to the compiler is only written "csc.exe", because they are in the same folder. But when I try to compile a test file written in C#, I get the error that the program can't find csc.exe. Do you know why this happens?

    I also want to launch csc.exe with an argument, the path to file that shall be compiled. How do I add that? If you had compiled the program manually using cmd.exe, you've done this:
    1. Changed the directory to where the file is.
    (cd <path to the folder where the file to be compiled is>)
    2. Typed csc <file name> (assuming you've added the folder where csc is to your environment variables).

    Now I want to just launch the csc.exe with the path to the file as an argument, using this method:

    Code:
    public void Compile()
            {
                System.Diagnostics.Process.Start(pathToCompiler + " " + Path);
            }
    To make a long text short, this is my problems:

    1. Why can't my program find an .exe-file in the same folder?
    2. How do I execute csc.exe with an argument?

    Sincerely yours,
    Saser
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    OK, to answer your number two question first:
    Code:
    Process proc = new Process();
    proc.StartInfo.FileName = "explorer.exe";
    proc.StartInfo.Arguments = "/select," + filepath; 
    proc.Start();
    There's a sample I gave someone else on how to use arguments. Make sure to either add using System.Diagnost ics; or qualify the name. This example simulates the console command:
    Code:
    explorer.exe /select,somefilename.txt
    As to your first question, I'm not sure. How about you try to provide the fully qualified name to the executable.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      If you check what this string produces:
      Directory.GetCu rrentDirectory( )
      (Either with a Console.WriteLi ne(Directory.Ge tCurrentDirecto ry()); or MessageBox.Show (Directory.GetC urrentDirectory ()); )
      You can see what directory the program is looking for csc.exe in, it might not be the directory that you think it will be.

      Comment

      Working...