【问题标题】:How to get parameters from cmd into a running WPF application如何从 cmd 获取参数到正在运行的 WPF 应用程序中
【发布时间】:2013-03-01 10:41:37
【问题描述】:

我有一个可以通过命令行获取参数的 WPF 应用程序。我的应用程序不能重复(我将其设为单个实例),但我希望能够多次添加参数 - 并将它们添加到 runnig 应用程序中而无需打开另一个窗口。 现在在我的代码中,我检查应用程序是否有另一个实例 - 如果有,我会抛出异常。 我正在寻找一种能够多次运行以下脚本的方法:

Myapp.exe -firstname first -lastname last

并且每次运行的应用程序都会将插入的参数添加到其列表中。 我该怎么做?

【问题讨论】:

    标签: c# wpf cmd parameter-passing


    【解决方案1】:

    您可以覆盖 App 类的 OnStartup 方法。请看here。您将在方法的参数中找到命令行参数。请看here

    public class App : Application
    {
        protected override OnStartup(StartupEventArgs e)
        {
            var cmdLineArgs = e.Args;
            // your logic here
        }
        // ...
    }
    

    由于您需要一些进程间通信机制,您可能应该阅读此SO post。我认为创建 WCF 服务是最好的选择。

    【讨论】:

    • 但是我的 MainWindow 类(运行应用程序的类)不能从 Application 继承。
    • Ô_o 没错,但 WPF 应用程序的入口点不是 MainWindow。它是 App 类,通常可以在 App.xamlApp.xaml.cs 中找到,您可以在其中实现覆盖。其余的应该很容易。
    • 我有办法从 OnStartApp 方法中退出应用程序吗?应用程序继续到 MainWindow() 构造函数。
    • 根据this 是的。 Google is your friend ;o)
    • 当我使用它时 - 如果我指向我的 MainWindow xaml - 另一个窗口是打开的。但我想将我作为参数获得的详细信息添加到当前运行的窗口中。
    【解决方案2】:

    在检查应用程序是否为单一应用程序时——如果存在应用程序实例(重复调用)——应用程序应该向用户发送错误消息。在这种情况下,您应该检查是否有命令行参数,如果有 - 只需发送 Close() 并返回命令而不显示任何错误消息。应用程序将从命令行获取参数并使用它们执行它所知道的操作。

    【讨论】:

      【解决方案3】:

      您可以使用 Application.Current.Shutdown() 来停止您的应用程序。如果在 OnStartup 中调用,它可以附加在显示的窗口之前。

      对于读取参数,您可以在任何地方使用 OnStartup 或 Environment.GetCommandLineArgs() 中的 e.Args。

      public partial class App : Application
      {
          protected override void OnStartup(StartupEventArgs e)
          {
              // check if it is the first instance or not
              // do logic
      
              // get arguments
              var cmdLineArgs = e.Args;
      
              if (thisisnotthefirst)
              {
                  // logic interprocess
                  // do logic
      
                  // exit this instance
                  Application.Current.Shutdown();
                  return;
              }
      
              base.OnStartup(e);
          }
      
          protected override void OnExit(ExitEventArgs e)
          {
              // may be some release needed for your single instance check
      
              base.OnExit(e);
          }
      }
      

      我不知道您如何检查单个实例,但我为此使用 Mutex:

          protected override void OnStartup(StartupEventArgs e)
          {
              Boolean createdNew;
              this.instanceMutex = new Mutex(true, "MySingleApplication", out createdNew);
              if (!createdNew)
              {
                  this.instanceMutex = null;
                  Application.Current.Shutdown();
                  return;
              }
      
              base.OnStartup(e);
          }
      
          protected override void OnExit(ExitEventArgs e)
          {
              if (this.instanceMutex != null)
              {
                  this.instanceMutex.ReleaseMutex();
              }
      
              base.OnExit(e);
          }
      

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-11
        • 2014-09-03
        • 2021-07-16
        • 2019-06-28
        • 1970-01-01
        • 2023-01-10
        • 2017-04-25
        • 1970-01-01
        相关资源
        最近更新 更多