将命令行参数从 VB 传递到 C# WPF 应用程序
Passing Command Line Arguments from VB to C# WPF Application
我有一个 C# WPF 应用程序,我希望能够从另一个用 VB.net 编写的现有应用程序打开它。就 c# 应用程序而言,我想我知道如何以两种不同的方式获取传递给它的命令行参数,这是我在研究 google 和使用其他人的答案时得到的。
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 方法 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();
}
}
上述方法将导致应用程序打开,但会填充 none 依赖于参数的数据。所以视图中的组合框和内容都是空的。失败了。
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) + "\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 execute 将参数强制为命令行参数。我也在 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
任何见解将不胜感激!谢谢。
我看到你在使用“\Microsoft\ChallengeHandler.appref-ms”。这是一个 ClickOnce 应用程序。获取 ClickOnce 应用程序的参数与普通应用程序完全不同。您需要使用 ApplicationDeployment.CurrentDeployment.ActivationUri.Query 和 HttpUtility.ParseQueryString 来检索它们。
我相信要发送它们,您必须使用“?param=value”将它们添加到启动 url 中。我只试过从网页启动它,所以我不确定它是否是这样工作的。
您当前使用的方法可以正常使用。如果你能找到 exe 并直接启动它,你应该没问题。
我创建了 2 个项目:Line 命令 C# 调用程序和一个 WPF 测试应用程序;
Program.cs 上的调用程序代码:
namespace WPFInvoker
{
class Program
{
static void Main(string[] args)
{
Process.Start(@"C:\Users\...\bin\Debug\WPF_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 时,会显示一个带有消息 "e.Args is null":
的消息框
Application without parameters
但是如果我通过 WPFInvoker 打开 WPFTest 应用程序:
Application with parameter
最后我关闭了 MessageBox 并显示了 MainWindow.xaml。
我有一个 C# WPF 应用程序,我希望能够从另一个用 VB.net 编写的现有应用程序打开它。就 c# 应用程序而言,我想我知道如何以两种不同的方式获取传递给它的命令行参数,这是我在研究 google 和使用其他人的答案时得到的。
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 方法 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();
}
}
上述方法将导致应用程序打开,但会填充 none 依赖于参数的数据。所以视图中的组合框和内容都是空的。失败了。
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) + "\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 execute 将参数强制为命令行参数。我也在 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
任何见解将不胜感激!谢谢。
我看到你在使用“\Microsoft\ChallengeHandler.appref-ms”。这是一个 ClickOnce 应用程序。获取 ClickOnce 应用程序的参数与普通应用程序完全不同。您需要使用 ApplicationDeployment.CurrentDeployment.ActivationUri.Query 和 HttpUtility.ParseQueryString 来检索它们。
我相信要发送它们,您必须使用“?param=value”将它们添加到启动 url 中。我只试过从网页启动它,所以我不确定它是否是这样工作的。
您当前使用的方法可以正常使用。如果你能找到 exe 并直接启动它,你应该没问题。
我创建了 2 个项目:Line 命令 C# 调用程序和一个 WPF 测试应用程序;
Program.cs 上的调用程序代码:
namespace WPFInvoker
{
class Program
{
static void Main(string[] args)
{
Process.Start(@"C:\Users\...\bin\Debug\WPF_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 时,会显示一个带有消息 "e.Args is null":
的消息框Application without parameters
但是如果我通过 WPFInvoker 打开 WPFTest 应用程序:
Application with parameter
最后我关闭了 MessageBox 并显示了 MainWindow.xaml。