Hello guys,

I'm trying to develop a application that calls another exe from my code base. The method to execute is pasted below...

private void mymethod(FileInfo file)
            {

                ProcessStartInfo startInfo = new ProcessStartInfo("jsmin.exe");
                startInfo.Arguments = "<fulljslint.js >jslint.js"; 
                
                Process p = new Process();
                p.StartInfo = startInfo;
                p.Start();
                p.WaitForExit();
            }

The issue is the command line arguemnts produce the following output when run..
//<fulljslint.js
//>jslint.js

Why is this? Its like its putting "\n" characters within the arguments.

The calling application is...
http://www.crockford.com/javascript/jsmin.html

thanks

It is splitting the arguments into two, like you show. It does that because of the space. Command line arguments are always separated around whitespace. To get it as one argument, you'll need to enclose it in quotes, like this:

startInfo.Arguments = @"""<fulljslint.js >jslint.js""";

The double quote is the escape sequence for quote. I know it looks funny, but try it :)

Wait what?

Did I just hear the double quote is the escape sequence for quote?

Whatever happened to:
" \" "
and
" \' "
??

Did I just hear the double quote is the escape sequence for quote?

When using a literal string (The @" " type string) you can't escape the " with a \, so you use double quotes "".

For non-literal strings, \" works fine.

I tend to make command line arguments literal strings as they often include path information.

Edited 5 Years Ago by Momerath: n/a

Hm, its given me progress but it hasn't given me the result i wanted.

Instead I now get \\<fulljslint.js >jslint.js

Rather than the application running...

It makes no sense, if I run this on a cmd prompt it works fine!

On the command line the "<" and ">" are used to redirect the input and output streams.
You need to tell the process to do the same.
This is not tested but will point you in the right direction.

private void mymethod(FileInfo file)
{
	// set up ProcessStartInfo with redirected I/O
	ProcessStartInfo startInfo = new ProcessStartInfo("jsmin.exe");
	startInfo.RedirectStandardInput = true;
	startInfo.RedirectStandardOutput = true;
	// create process and start
	Process p = new Process();
	p.StartInfo = startInfo;
	p.Start();
	// get I/O streams
	var datainput = p.StandardInput;
	var dataoutput = p.StandardOutput;
	// write file contents to the input stream
	datainput.Write(File.ReadAllText("fulljslint.js"));
	// read all output stream data to a file 
	File.WriteAllText("jslint.js", dataoutput.ReadToEnd());
	// confirm process has exited
	p.WaitForExit();
}

Note: You will have to add the full path to your input and output file names!
Hope this helps.
[EDIT]
You might have to break the read/write process in to chunks using a loop. Especially if the files are big.

Edited 5 Years Ago by nick.crane: Changed order of Start and getting I/O streams!

Hmm thanks for the code nick, but for the life of me I can't get it to work. It freezes at File.WriteAllText("jslint.js", dataoutput.ReadToEnd());
and i dont know why

This article has been dead for over six months. Start a new discussion instead.