从VB向C#WPF应用程序传递命令行参数



我有一个C#WPF应用程序,我希望能够从另一个现有的应用程序中打开,该应用程序是在VB.net中编写的。就C#应用程序而言,我想我知道如何获得通过两种不同方式传给它的命令行参数,这是我在研究谷歌和使用其他人的答案时获得的。

App.xaml标头

<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方法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();
}
}

上面的方法将导致应用程序打开,但没有填充任何依赖于参数的数据。所以视图中的组合框和其他东西都是空的。这是失败的。

App.xaml.cs方法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();
}
}

由于参数为空,此方法每次只显示错误消息框。

我有一种感觉,问题是当我试图从VB.NET应用程序打开应用程序并从那里将字符串参数传递给c#应用程序时。但是我不知道如何从VB.net代码中传递一个类似命令行参数的字符串。从VB.net应用程序调用

Dim sPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Programs) + "MicrosoftChallengeHandler.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

我已经尝试了没有的VB代码

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

但无济于事;我添加它们是希望shell执行能够强制将这些参数作为命令行参数。我也在VB中尝试过:第二种VB方法

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

如有任何见解,我们将不胜感激!谢谢

我看到您使用的是"\Microsoft\ChallengeHandler.appref-ms"。这是一个ClickOnce应用程序。获取ClickOnce应用程序的参数与普通应用程序完全不同。您需要使用ApplicationDeployment.CurrentDeployment.ActivationUri.Query和HttpUtility.ParseQueryString来检索它们。

我相信要发送它们,您必须使用"?param=value"将它们添加到启动url中。我只试过从网页上启动它,所以我不确定它是否就是这样工作的。

您当前使用的方法对正常应用程序有效。如果你能找到exe并直接启动它,你应该没问题。

我创建了两个项目:Line命令C#调用程序和一个WPF测试应用程序;

Program.cs:上的调用程序代码

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

然后在WPF App.xaml上,我有启动事件App_startup和主窗体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>

App.xaml.cs的代码是:

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");
}
}
}
}

当我双击打开WPFTest.exe时,会显示一个MessageBox,其中包含消息"e.Args is null":

无参数应用

但是如果我通过WPFInvoker:打开WPFTest应用程序

带参数的应用

最后,我关闭MessageBox并显示MainWindow.xaml。

最新更新