【问题标题】:Start A Process With Parameters使用参数启动进程
【发布时间】:2011-08-11 14:45:35
【问题描述】:

我正在使用 Process.Start 从我的网站打开我用 c# 制作的 Windows 窗体应用程序。

我想向应用程序发送我的用户名。

那我该怎么做呢?

【问题讨论】:

  • 如果您在网站上使用Process.Start(),它将在您的服务器上打开该应用程序。这真的是你想要的吗?
  • 这是一个学校项目,我知道不应该这样,但这是他们现在要求我们做的。

标签: c# asp.net winforms parameters process


【解决方案1】:

您可以通过在开始信息中分配参数来做到这一点,例如:

var process = new Process
      {
          StartInfo =
              {
                  FileName = processName,
                  Arguments = "-username=Alice"
              }
      };
process.Start();

如果您的进程无法启动,您可能需要检查权限,据我所知,在 IIS 上运行的代码是不允许这样做的。

【讨论】:

    【解决方案2】:

    Process.Start() 有几个重载,one of them 用于指定命令行参数以及可执行文件的路径。

    例如:

    Process.Start("app.exe", "parameter(s)");
    

    【讨论】:

    【解决方案3】:

    你可以用这个:

    Process.Start("MyExe.exe", "arguments");
    

    【讨论】:

    • 我如何访问我的应用程序上的用户名?
    • 这行不通,因为不会有名为MyExe.exe"username"的文件。
    • 只需使用string myUsername = Environment.GetCommandLineArgs()[1].ToString();,它会为您提供您在上面作为参数输入的内容。
    • @svick,我现在更正了 "MyExe.exe" + " \"" + username + "\"" 将准确地编译为 MyExe.exe "username"。你可以看到我在参数中加入了转义的 \" 字符。
    • 是的,一开始我没有注意到逃逸。但它仍然无法以这种方式工作,您必须将命令行参数作为单独的参数放入Process.Start()。此外,Path.Combine() 不会在您的代码中执行任何操作。
    【解决方案4】:

    你去,应该可以工作

    using System;
    using System.Runtime.InteropServices;
    using System.Threading;
    using System.Windows.Forms;
    
    namespace SSHPit
    {
    
    public partial class MainForm : Form
    {       
        [DllImportAttribute("user32.dll")]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
    
    
        public MainForm()
        {           
            InitializeComponent();
            
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = "L:\\Program Files\\putty\\putty.exe";
            p.StartInfo.Arguments = "-load \"mysession\" -ssh 127.0.0.1";
            p.Start();
            p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
            p.WaitForInputIdle();
            while (p.MainWindowHandle == IntPtr.Zero)
            {
               Thread.Sleep(100); 
               p.Refresh();
            }
            SetParent(p.MainWindowHandle, panel1.Handle);
        }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-13
      • 1970-01-01
      相关资源
      最近更新 更多