Resolved Is it possible to run some args without taking a shortcut?

lunok

New member
Joined
Jan 1, 2021
Messages
2
Location
France
Programming Experience
1-3
Hello,

I've an .exe program that takes 2 arguments at the start. When I create a shortcut to this program and add these 2 arguments to the target it works fine. But when I try to run this program with the same arguments directly on the source target it doesn't work, the arguments are not taken into account.

Source target: "C:\Program Files (x86)\test\Test.exe"
Shortcut: "C:\Program Files (x86)\Test.exe.lnk" /a2 87549

The program "Test.exe" starts by clicking on the "Start Program" button.

Code :
C#:
/*--------- TARGET SOURCE CODE (DOESN'T WORKS) ----------*/
string fileName = @"C:\Program Files (x86)\Test\Test.exe";
ProcessStartInfo startInfo = new ProcessStartInfo(fileName);
startInfo.Arguments = "/a2 87549";
Process proc = Process.Start(startInfo);

/*--------- SHORTCUT CODE (WORKS) ----------*/
string fileName = @"C:\Program Files (x86)\Test.exe.lnk";
ProcessStartInfo startInfo = new ProcessStartInfo(fileName);
startInfo.Arguments = "/a2 87549";
Process proc = Process.Start(startInfo);

Can I even do what i'm trying to do? (Windows 10 64bits, .NET Framework 4.7.2, Windows Form).

Thank you for your help.
 
Does it work in a command prompt? If you open a command window and type "C:\Program Files (x86)\Test.exe.lnk" /a2 87549, does it work as you want? I would expect not. If I'm not mistaken, a shortcut actually contains the commandline arguments itself, rather than their being passed to it. If it doesn't work in a command prompt then it won't work in .NET.

I'm not sure that it would make any difference but you might also try it with UseShellExecute set to different values. I think that true is the default for .NET Framework apps so, if it was going to work, I'd expect that that would be what was required but you could try false as well, just in case.
 
I would think both "work" but not as you expected. The difference is probably that the shortcut has WorkingDirectory set. The behaviour for ProcessStartInfo and WorkingDirectory depends on UseShellExecute (and it can have different defaults in different types of projects), read all about it here:
 
Hello,
Thank you for your answers. I tried to run the program by the cmd window. The result is that when I write an absolute path like "C:\Program Files (x86)\Test\Test.exe" with my arguments, it doesn't work. But when I try with a relative path, it works well. Maybe it's not possible to put any arguments in an absolute path. I've for now a solution. Thank you for your help.

Edit : Ok, if I add the workingDirectory, it works with an absolute path. Thanks to you guys ! Yours both answers helped me.
 
I suspect that the program you are trying to launch needs the working directory set. I know that I can launch notepad.exe with arguments using Process.Start() without it needing me to set the working directory.
 
I suspect that the program you are trying to launch needs the working directory set.
Indeed. This is an example of why you should not use Environment.CurrentDirectory in your code. If you do, your app will also need the working directory set. If you want to access a file in your program folder then use something that always points to that folder, e.g. Application.StartupPath, rather than relying on someone else setting something to point to the right path.
 
Back
Top Bottom