【发布时间】:2018-11-16 08:05:53
【问题描述】:
我有一个 C# WPF 应用程序,我希望能够从另一个用 VB.net 编写的现有应用程序打开它。就 c# 应用程序而言,我想我知道如何以两种不同的方式获取传递给它的命令行参数,这是我在研究 google 并使用其他人的答案时得到的。
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();
}
}
此方法每次只显示错误消息框,因为 args 为空。
我感觉问题是当我尝试从 VB.NET 应用程序打开应用程序并将字符串参数从那里传递给 c# 应用程序时。但是我不知道如何从 VB.net 代码中传递像命令行参数这样的字符串。 从 VB.net 应用程序调用
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
我试过没有
的 VB 代码pHelp.UseShellExecute = True
pHelp.WindowStyle = ProcessWindowStyle.Normal
无济于事;我添加它们是希望 shell 执行将强制参数作为命令行参数。我也在VB中试过这个: 第二个 VB 方法
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
任何见解将不胜感激!谢谢。
【问题讨论】:
标签: c# wpf vb.net parameter-passing command-line-arguments