Link to home
Start Free TrialLog in
Avatar of NDennisV
NDennisVFlag for United States of America

asked on

Cannot get StartInfo.Arguments to work

I cannot get  StartInfo.Arguments to work.

It runs the program okay but does not see the arguments.

System.Diagnostics.Process p = new Process();
p.StartInfo.WorkingDirectory = @"C:\Users\dennis\Documents\Visual Studio 2005\Projects\LocationMaintenance\LocationMaintenance\publish\";
p.StartInfo.FileName = @"LocationMaintenance.application";
p.StartInfo.Arguments = "dennis";
p.Start();

Open in new window


There are no spaces in the arguments.

You'll have to trust me that I need to that working directory.

I've searched the Internet for hours but cannot find a solution.

Thanks for any help.

Dennis
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

You don't actually supply a program that is to be run
Avatar of NDennisV

ASKER

p.StartInfo.FileName = @"LocationMaintenance.application";

Is the program to run. Am I missing something?

Like I said, the program runs but it does not see the arguments.


In the LocationMaintenance program I have:

string[] cmd = Environment.GetCommandLineArgs();

Open in new window

and these arguments work fine from a prompt.
There is nothing wrong with your implementation - took your code and did as follows
            System.Diagnostics.Process p = new Process();
            p.StartInfo.WorkingDirectory = @"S:\data\";
            p.StartInfo.FileName = @"C:\Windows\notepad.exe";
            p.StartInfo.Arguments = "test.txt";
            p.Start();

Open in new window


Works fine - so your problem is either in your application - or in the working folder (depends on how the application is interpreting your arguments - file - string etc).
You are aware that GetCommandLineArgs is different than Main(string[] args), I hope. The former includes the program name in index 0, and the first argument is actually in index 1; the latter does not have the program name as one of its entries, and the first argument is at index 0.
LocationMaintenance.application - you say that is a program that runs by itself.  (I must be getting old - I thought it required either exe or com as extension)
I can get simple things like you posted to run too. I just cannot get this program to see the arguments.

I just tried

static void Main(string[] args)
        {
            for (int i = 0; i < args.Length; ++i)
            {
                MessageBox.Show(args[i]);
            }
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

Open in new window

and the program is not seeing the arguments.
@AndyAinscow

IIRC, you get an "application" extension if you use ClickOnce deployment  : )
Sorry Andy, I don't know what you mean?

I've tried LocationMaintenance.exe and it does not run.
As far as I know, the *.application file is not actually your program, but more like a manifest. You can open it up in a text editor and see that it is XML. Should I assume that the reason you are wanting to target the .application file is so that you can ensure any updates to the application are retrieved prior to the app running?

The explanation as to why the application does not see the arguments, so far as I can see, is because when you run the .application file, you are not actually running your program. You are running whichever application is registered to execute .application files (I do not know its name off the top of my head). For this reason, you are passing your arguments to that application, and not actually to the application you wish to target.
Avatar of vinhnl
vinhnl

You sould use String[] instead of string[]

String[] arguments = Environment.GetCommandLineArgs();
Or use stringbuilder like that

  
System.Text.StringBuilder strbuilder= new System.Text.StringBuilder();


    foreach (String arg in Environment.GetCommandLineArgs())
    {
        strbuilder.AppendLine(arg);
        barcode = arg;
    }
    psnfo = strbuilder.ToString();

Open in new window

In the publish folder there is only the application file and there is no exe file except setup.exe.

I just tried   p.StartInfo.FileName = @"setup.exe";   and it also runs the program but still no arguments.
Juat tried    String[] instead of string[]     and still not getting the arguments.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks  kaufmed. It's lengthy and I'm burned out after working on this for 5 hours straight.

Looks like what I need and I think I now understand the problem with click once.

I'll get on it again after a few hours rest or tomorrow.

Thanks again.
I tried for many hours to get this to work and ended up writing a script file that does the same thing.

kaufmed's post was good and led me to understand that this is not always possible. Thanks.