[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 582
  • Last Modified:

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
0
NDennisV
Asked:
NDennisV
  • 7
  • 4
  • 2
  • +2
1 Solution
 
AndyAinscowCommented:
You don't actually supply a program that is to be run
0
 
NDennisVAuthor Commented:
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.
0
 
Julian HansenCommented:
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).
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
käµfm³d 👽Commented:
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.
0
 
AndyAinscowCommented:
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)
0
 
NDennisVAuthor Commented:
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.
0
 
käµfm³d 👽Commented:
@AndyAinscow

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

I've tried LocationMaintenance.exe and it does not run.
0
 
käµfm³d 👽Commented:
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.
0
 
vinhnlCommented:
You sould use String[] instead of string[]

String[] arguments = Environment.GetCommandLineArgs();
0
 
vinhnlCommented:
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

0
 
NDennisVAuthor Commented:
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.
0
 
NDennisVAuthor Commented:
Juat tried    String[] instead of string[]     and still not getting the arguments.
0
 
NDennisVAuthor Commented:
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.
0
 
NDennisVAuthor Commented:
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.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 7
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now