1

I have a C# WPF application which I want to be able to open from another existing application, which was written in VB.net. As far as the c# application goes, I think I know how to get command line parameters that are passed to it two different ways, which I got while researching google and using others' answers.

App.xaml Header

<Application x:Class="ChallengeHandler.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:ChallengeHandler"
         Startup="Application_Startup">

App.xaml.cs Method 1

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        string[] args = Environment.GetCommandLineArgs();
        if (args.Length < 1)
        {
            MessageBox.Show("No parameter provided. Failed to run.");
            Shutdown();
        }
        else
        {
            MainWindow wnd = new MainWindow(args[0]);
            wnd.Show();
        }
    }

The above method will result in the application opening but none of the data, which relies on the parameter, is populated. So the comboboxes and stuff in the views are just empty. That fails.

App.xaml.cs Method 2

    private void Application_Startup(object sender, StartupEventArgs e)
    {

        if (e.Args.Length < 1)
        {
            MessageBox.Show("No parameter provided. Failed to run.");
            Shutdown();
        }
        else
        {
            MainWindow wnd = new MainWindow(e.Args[0]);
            wnd.Show();
        }
    }

This method just shows the error messagebox each time, as the args is empty.

I have a feeling the issue is when I'm trying to open the application from the VB.NET application and pass the string parameter to the c# app from there. But I am out of ideas on how to pass a string like a command line parameter from the VB.net code. Calling from a VB.net Application

    Dim sPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Programs) + "\Microsoft\ChallengeHandler.appref-ms"
    Dim pHelp As New ProcessStartInfo

    If System.IO.File.Exists(sPath) Then
        pHelp.FileName = sPath
        pHelp.Arguments = "097"
        pHelp.UseShellExecute = True
        pHelp.WindowStyle = ProcessWindowStyle.Normal
        Dim proc As Process = Process.Start(pHelp)
    End If

I have tried the VB code without the

pHelp.UseShellExecute = True
pHelp.WindowStyle = ProcessWindowStyle.Normal

with no avail; I added them in the hope the shell execute would force the parameters as command line parameters. I have also tried this in VB: 2nd VB Method

    Dim sPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Programs) + "\Microsoft\ChallengeHandler.appref-ms"
    If System.IO.File.Exists(sPath) Then
        System.Diagnostics.Process.Start(sPath, "097")
    End If

Any insight would be GREATLY appreciated! Thanks.

2 Answers 2

1

I see you're using "\Microsoft\ChallengeHandler.appref-ms". This is a ClickOnce application. Getting the parameters for a ClickOnce application is completely different from a normal application. You need to use ApplicationDeployment.CurrentDeployment.ActivationUri.Query and HttpUtility.ParseQueryString for retrieving them.

I believe to send them across you'll have to add them to the launch url by using "?param=value". I've only tried launching it from a web page, so I'm unsure if this is how it works.

The method you're currently using is valid for normal application. If you can locate the exe and launch that directly, you should be fine.

Sign up to request clarification or add additional context in comments.

1 Comment

yeah after doing further research, seems it's quite difficult for an offline only application. I'm going to think of a different way or just not implement that feature right now. It's not needed, I was just trying to make it robust so it could be used between plant locations. Thanks!
1

I created 2 projects: Line command C# invoker and a WPF Test application;

The invoker code on Program.cs:

    namespace WPFInvoker
    {
        class Program
        {
            static void Main(string[] args)
            {
                Process.Start(@"C:\Users\...\bin\Debug\WPF_Test.exe", "example_parameter");
            }
        }
    }

Then on the WPF App.xaml I have the startup event app_Startup and the main form MainWindow.xaml:

    <Application x:Class="WPF_Test.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:WPF_Test"
         Startup="app_Startup"
         StartupUri="MainWindow.xaml">
        <Application.Resources>

        </Application.Resources>
    </Application>

And the App.xaml.cs code is:

    namespace WPF_Test
    {
        /// <summary>
        /// Interaction logic for App.xaml
        /// </summary>
        public partial class App : Application
        {
            void app_Startup(object sender, StartupEventArgs e)
            {
                if (e.Args != null && e.Args.Length > 0)
                {
                    MessageBox.Show(e.Args[0]);
                }
                else
                {
                    MessageBox.Show("e.Args is null");
                }
            }
        }
    }

When I open the WPFTest.exe with a double click shows a MessageBox with the message "e.Args is null":

Application without parameters

But if I open the WPFTest application through WPFInvoker:

Application with parameter

Finally I close the MessageBox and the MainWindow.xaml its shown.

1 Comment

This is a good example for when I an calling the executable. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.