【问题标题】:Using process.start in a wpf application to invoke another wpf application在 wpf 应用程序中使用 process.start 来调用另一个 wpf 应用程序
【发布时间】:2012-05-19 19:18:22
【问题描述】:

我正在尝试从另一个 wpf 应用程序调用一个 wpf 应用程序。 调用 wpf 应用程序进行调用

ProcessStartInfo BOM = new ProcessStartInfo();

BOM.FileName = @"D:\WPFAPPLICATION.exe";

BOM.Arguments = temp;

Process.Start(BOM);

现在在调用的应用程序中,我尝试检索使用

传递的参数
  string arguments =Process.GetCurrentProcess().StartInfo.Arguments;

但是参数没有被传递。这是为什么呢??

我还尝试了另一种方法,其中:

    public partial class App : Application
    {
    public static String[] mArgs;

    private void Application_Startup(object sender, StartupEventArgs e)
    {

        if (e.Args.Length > 0)
        {
            mArgs = e.Args;


        }
    }
    }
    }

但是这也不起作用!!! 请帮忙!!

【问题讨论】:

  • 您确定temp 确实具有您期望的价值吗?此外,在使用 Powershell 之前,我在从启动信息中检索参数时遇到了问题,忘记了问题所在,但我认为应用程序仍然使用正确的参数运行,它可能只是没有保留在启动信息中。
  • 我确定 temp 保持所需的值。但我不认为它被传递给被调用的 wpf 应用程序。
  • @Sana,你能出示temp的声明吗?
  • var temp = string.Empty; temp = Convert.ToString(values[0], CultureInfo.InvariantCulture);我非常确定 temp 保持所需的值,因为我添加了一块手表并监控了更改。

标签: c# wpf process argument-passing calling-convention


【解决方案1】:

尝试使用Environment 类来获取命令行参数。

string[] args = Environment.GetCommandLineArgs

或使用传递给 WPF 应用程序 (App.xaml.cs) 的主要方法的字符串 []。

public partial class App : Application {

    protected override void OnStartup(StartupEventArgs e) {
        string[] args = e.Args;
    }
}

注意: 来电

string arguments =Process.GetCurrentProcess().StartInfo.Arguments;

不会返回任何值。看到这个MSDN 条目

如果您没有使用 Start 方法启动进程,则 StartInfo 属性不会反映用于启动进程的参数。例如,如果您使用 GetProcesses 获取计算机上运行的进程数组,则每个 Process 的 StartInfo 属性不包含用于启动进程的原始文件名或参数。

【讨论】:

  • 但我确实在调用应用程序中使用了 start 方法来启动进程
  • 我使用了您之前建议的类似方法(请参阅编辑)...但这也不起作用。
  • @Sana 但不是从启动的应用程序内部
  • 我之前也试过 string[] args = Environment.GetCommandLineArgs ..我在死胡同!!
  • @Sana,这正是问题所在。你有两个进程,它们显然不共享变量;每个都有自己的 Process 对象实例。第一个进程中的对象(调用Start 的对象)将具有StartInfo。第二个进程中的对象不会,因为它正在为 already-running 进程获取Process 实例。 GetCurrentProcess 在这方面等同于 GetProcesses:你没有开​​始一个 new 进程,你正在获得一个 existing 进程,所以没有@ 987654332@(根据本答案末尾引用的 MSDN 文档)。
【解决方案2】:

好吧,如果有人感兴趣,我终于找到了我的问题的解决方案。 在调用应用程序中,我维护了之前使用的相同代码:

ProcessStartInfo BOM = new ProcessStartInfo();
BOM.FileName = @"D:\WPFAPPLICATION.exe";
BOM.Arguments = temp;
Process.Start(BOM);

在被调用的应用程序中,为了成功接收参数,我只需要:

    System.Text.StringBuilder strbuilder= new System.Text.StringBuilder();


    foreach (String arg in Environment.GetCommandLineArgs())
    {
        strbuilder.AppendLine(arg);
        barcode = arg;
    }
    psnfo = strbuilder.ToString();

我没有以正确的方式处理传递给进程的参数

所以在显示psnfo时

代码返回:

 D:\WPFAPPLICATION.exe temp

来源:http://www.codeproject.com/Questions/386260/Using-process-start-in-a-wpf-application-to-invoke

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多