3

I have a WinForms application that accepts a command line argument by calling Environment.GetCommandLineArgs() and does something with it.

It works fine in Debug mode - I enter the argument in the Debug tab of Project Properties, then run it (F5) and the application gets the argument correctly.

But after I publish the application and try to call it from another Winform application with this code line:

Process.Start("\\path\to\myApp\MyApp.application", "4")

it doesn't work. Apparently the argument is not passed to the application for some reason, and I don't know why. I also tried to create a new process and set its ProcessStartInfo.Arguments before starting it, but it still doesn't work.

Can anyone help me?

UPDATE

It seems to me that when Process.Start("\\path\to\etc", "4") is called, what is actually run is the local copy of the program on my machine, located at C:\users\myUserName\AppData\Local\App\2.0\long-string-of-digits-and-letters\MyA‌​pp.exe. If I run Process.Start("C:\users\etc", "4") instead - it works.

Now my question is - why wasn't the argument passed to the local copy of the program when running Process.Start("\\path\to\etc", "4")? What should I do so that the argument is passed to the local copy?

kodkod
  • 1,556
  • 4
  • 21
  • 43
  • GetCommandLineArgs returns a string array where the first element is the name of the program and, starting from the second index the arguments. Are you sure you get the right index inside your application? Could you show how do you handle this argument? – Steve Apr 07 '13 at 11:04
  • Try debugging along the lines suggested by Steve by outputting the results of `Environment.CommandLine`. What do you see in both scenarios? How does it differ, if at all? – Cody Gray - on strike Apr 07 '13 at 11:06
  • @Steve I know. I first check `Environment.GetCommandLineArgs().Length > 1` to see if there is an argument, then use `Environment.GetCommandLineArgs()(1)` to get it. – kodkod Apr 07 '13 at 11:21
  • I've noticed that the 0-index argument (the program path) appears to be something like `C:\users\myUserName\AppData\Local\App\2.0\long-string-of-digits-and-letters\MyApp.exe`, which is different from the `\\path\to\myApp\MyApp.application` in `Process.Start()`. I don't know if it's significant and/or relevant to my problem. – kodkod Apr 07 '13 at 11:42

1 Answers1

1

OK, as I wrote in the update of the question, the problem was in the link. Presumably the .application file ran the local .exe file on my machine, but for some reason didn't pass in the argument to it. I had to open directly the local .exe file.

To solve this problem, I added code along these lines (validation/exception handling logic omitted for brevity):

Dim path As String = Environment.GetEnvironmentVariable("LOCALAPPDATA") & "\apps\2.0"
Dim files() As String = Directory.GetFiles(path, "MyApp.exe", IO.SearchOption.AllDirectories)
Process.Start(files(0), "4")

Not an optimal solution (since it assumes that the local .exe file is located in some subfolder of %LOCALAPPDATA%\apps\2.0), but at least it solved my problem...

kodkod
  • 1,556
  • 4
  • 21
  • 43