-1

I have tried absolutely everything I can think of to execute an external legacy App I have through C#, but it just won't work properly unless I manually use the command prompt. So far, I have tried:

a) Executing the process directly:

// Path to Legacy App;

var procName = @"C:\Path to Exe\With lots of Spaces\In the name.exe";

//Happens to be a path to a text file

var arg1 = @"C:\Long Path Name\With Spaces to\Input File Name.txt";

//Legacy Exe uses this char to separate 'source' and 'target' text files.

var arg2 = " > ";

// Name of the new text file to create, once processed by procName

var arg3 = @"C:\Long Path Name\With Spaces to\Input File Name_new.txt";

Process.Start(procName, arg1 + arg2 + arg3); => FAILS

b) I tried calling cmd.exe and then feeding it the exe path and the args:

// Path to Legacy App

var arg1 = @"C:\Path to Exe\With lots of Spaces\In the name.exe";

//Happens to be a path to a text file

var arg2 = @"C:\Long Path Name\With Spaces to\Input File Name.txt";

//Legacy Exe uses this char to separate 'source' and 'target' text files.

var arg3 = " > ";

// Name of the new text file to create, once processed by procName

var arg4 = @"C:\Long Path Name\With Spaces to\Input File Name_new.txt";

var allArgs = arg1 + arg2 + arg3 + arg4;

Process.Start("cmd.exe", allArgs); => FAILS

I have tried everything I can think of, including adding /k, /s or both before calling the initial exe, wrapping everything in quotes, wrapping each argument in quotes, etc. I even used an 8.3 shortened path name for the path to the legacy exe since the command prompt would sometimes say it could not find the app and would stop at the first space.

Of course, if I open a command prompt and I copy-paste the contents of allArgs into cmd and execute, the external app runs properly and I get my modified text file.

If anyone has any pointers or suggestions I am happy to explore all avenues. I am at my wits end.

This should not be this hard.

Thank you!

Rand Random
  • 7,300
  • 10
  • 40
  • 88
CSharpBear
  • 41
  • 1
  • 6
  • As for the EXE path itself, have you tried [Windows' long-file-name format](https://web.archive.org/web/20160818035729/https://blogs.msdn.microsoft.com/jeremykuhne/2016/06/21/more-on-new-net-path-handling/)? (`\\?\{DriveLetter}:\{Path}`) e.g. use `Process.Start( @"\\?\C:\Path to Exe\With lots of Spaces\In the name.exe", args )` – Dai Jun 16 '23 at 22:18
  • 1
    You will definitely need to wrap each argument in double-quotes, as that's how pretty-much every OS and program handles spaces-in-paths - please make that change to your code in Example B, and then show us what the actual value of `allArgs` is. – Dai Jun 16 '23 at 22:20
  • 2
    The program's use of `>` looks like it's actually using `stdout` redirection via the shell instead of actually using `">"` as a command-line argument - in which case you _can_ still use `Process.Start` but you will need to set-up stdout redirection which is a pain in C#/.NET so I recommend putting everything in a `runThis.cmd` file you run via `Process.Start( "cmd.exe", "/k runThis.cmd" );`. – Dai Jun 16 '23 at 22:21
  • 1
    Here some info about what @Dai said https://www.rushis.com/windows-command-prompt-redirecting-stdoutstderr/ | https://stackoverflow.com/questions/1420965 - wanted to point it out aswell – Rand Random Jun 16 '23 at 22:27
  • @Dai: I had previously wrapped everything in quotes, but it didn't work. I do want to thank you for letting me know that redirection would be a pain. It led me to creating a bat file. In the end, only that approach worked. – CSharpBear Jun 19 '23 at 18:54

1 Answers1

0

Leaving this for anyone having similar trouble. I ended up creating a .bat file accepting 2 arguments:

  1. Path to initial file
  2. Path to the redirected file

The contents of the bat file were absolutely simple:

*LongPathNameToExeWrappedInQuotes* %1 > %2

I then called a process passing a ProcesssStartInfo:

var batFileName = "\"C:\\Path To Bat\\File Name\\Long Name and\\Spaces.bat\" ";
var arg1 = "\"C:\Long Path Name\With Spaces to\Input File Name.txt\"";
var arg2 = " \"C:\Long Path Name\With Spaces to\Input File Name_new.txt\"";
var finalString = batFileName + arg1 + arg2; 
var psi = new ProcessStartInfo(finalString);
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
var runExternalApp = Proces.Start(psi);
runExternalApp.WaitForExit();

Viola! Problem solved.

Notice I did have to wrap the bat file name as well as the individual file names in chr(34). I also manually added the spaces needed to separate the call to the bat file and the 2 args.

CSharpBear
  • 41
  • 1
  • 6