9

我正在尝试从另一个 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;


        }
    }
    }
    }

然而这也不起作用!!!请帮忙!!

4

2 回答 2

3

尝试使用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 属性不包含用于启动进程的原始文件名或参数。

于 2012-05-11T07:51:45.087 回答
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

于 2012-05-17T09:28:51.110 回答