Connecting Tech Pros Worldwide Help | Site Map

Executing a .exe-program with arguments

  #1  
Old July 9th, 2009, 03:30 PM
Newbie
 
Join Date: Jul 2009
Posts: 17
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:

Expand|Select|Wrap|Line Numbers
  1. public void Compile()
  2.         {
  3.             System.Diagnostics.Process.Start(pathToCompiler + " " + Path);
  4.         }
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
  #2  
Old July 9th, 2009, 03:39 PM
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,568

re: Executing a .exe-program with arguments


OK, to answer your number two question first:
Expand|Select|Wrap|Line Numbers
  1. Process proc = new Process();
  2. proc.StartInfo.FileName = "explorer.exe";
  3. proc.StartInfo.Arguments = "/select," + filepath; 
  4. proc.Start();
There's a sample I gave someone else on how to use arguments. Make sure to either add using System.Diagnostics; or qualify the name. This example simulates the console command:
Expand|Select|Wrap|Line Numbers
  1. 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.
  #3  
Old July 9th, 2009, 05:14 PM
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,089
Provided Answers: 3

re: Executing a .exe-program with arguments


If you check what this string produces:
Directory.GetCurrentDirectory()
(Either with a Console.WriteLine(Directory.GetCurrentDirectory()) ; or MessageBox.Show(Directory.GetCurrentDirectory()); )
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.
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing system calls K Viltersten answers 4 July 25th, 2008 10:05 PM
C# WEB: Running an .exe file Xexon answers 3 December 13th, 2007 12:39 PM
Intercept crash of an external exe lookaround answers 3 November 25th, 2006 09:35 AM
Executing "unknown" command line in C# Rune Jacobsen answers 1 January 2nd, 2006 09:45 PM
Executing a PERL script from ASP.NET appl. PzYon answers 3 July 21st, 2005 08:17 PM