0

i got a problem when passing two arguments from one WPF App to another WPF App. I publish the second WPF App to my Desktop and i want to start it with my first WPF App.

First-Program:

public MainWindow()
{
    InitializeComponent();
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = @"C:\Users\User\Desktop\Work.application";
    startInfo.Arguments = "test 1234";
    Process.Start(startInfo);
}

To get the arguments in the second program i tried the following code

1.Get Arguments in Mainwindow with Environment.GetCommandLineArgs() => doesn't work

public MainWindowSecondProgram()
{
    InitializeComponent();
    string[] args = Environment.GetCommandLineArgs();
    foreach (String element in args)
    {
        MessageBox.Show(element);
    }
}

2.Get Arguments in App by using startup function => doesn't work

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        foreach (string element in e.Args)
        {
            MessageBox.Show(element);
        }
    }
}

Now if i copy the Work.exe (not Work.application) from my Visual Studio Project folder to my Desktop and change the path from

@"C:\Users\User\Desktop\Work.application" to
@"C:\Users\User\Desktop\Work.exe"

and run my first program again, it works perfect with the first function and the second function.

So why is it working with the EXE but not with the published application?

Edit: I tested both functions by passing two arguments threw the debugger and it works, but not by passing it to the published application, only EXE works.

Cramble
  • 195
  • 3
  • 13
  • Possible duplicate of [C# ProcessStartInfo arguments](http://stackoverflow.com/questions/15454456/c-sharp-processstartinfo-arguments) – MethodMan Oct 08 '15 at 20:44
  • @MethodMan i did the same, but no results. think its a different problem. he is talking about passing to EXE and im talking about the passing to.application. (different?!) EXE works for me, but not for the application. – Cramble Oct 08 '15 at 22:46
  • why do you think `application` file can be treated as normal application? To me it's just a ***setup*** file. It's for convenience only and yes it's totally unprofessional (even publishing via web nowadays usually requires users to download a full-fledged setup file as `exe` or `msi` - it looks much more professional and users nowadays are smart enough to know how to use those setup files). Because it's just a quickly setup file, running it will run the setup first (prepare the exe file) before actually running the exe file. What you want is impossible and ***nonsense***. – Hopeless Oct 09 '15 at 02:51
  • @Hopeless why should it be nonsense? its an offline click-once application, which is published/changed around 2 times a week because of new features or changes. every time the agent start the application, it checks if there is a newer version and you dont need admin permissions to execute the app. Best regards – Cramble Oct 09 '15 at 08:37
  • I mean `application` file is out of your control, it's like a compiled package or setup file and cannot be understood as what you put in your application. I guess you may also want to pass some arguments to a `msi` file? It's a package rather than a program (of yours). When running setup file like `application` file, the logic in your code is not even run. In fact the actual exe will be called in sequence but the info about args will be omitted (I believe this is by design). – Hopeless Oct 09 '15 at 09:03
  • @Hopeless ok, now i got it. Thanks. There are several articles about that problem like https://robindotnet.wordpress.com/2010/03/21/how-to-pass-arguments-to-an-offline-clickonce-application/ – Cramble Oct 09 '15 at 10:12

2 Answers2

0

For a Windows Store App, you need to use Application.OnLaunched. Try this code:

public partial class App : Application
{
    protected override void OnLaunched(LaunchActivatedEventArgs args)
    {

            MessageBox.Show(args.Arguments);
    }
}

Note that you'll have to turn that string into an array yourself.

Dan Field
  • 20,885
  • 5
  • 55
  • 71
  • Filed: First thanks for your answer. The application is a desktop wpf application not a windows store app. Best regards – Cramble Oct 08 '15 at 22:31
  • Either way I believe if you're inheriting from `Application`, this is the way to get arguments. – Dan Field Oct 09 '15 at 13:33
0

To read arguments in a ClickOnce application, use:

AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData

You can read more here.

Also, not sure, but you may need to run the ClickOnce app via the .appref-ms shortcut as opposed to the .application file itself.

Dax Pandhi
  • 843
  • 4
  • 13