C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > c# process 参数

浅析C# 使用Process调用外部程序中所遇到的参数问题

作者:暗暗大人

这篇文章主要介绍了C# 使用Process调用外部程序中所遇到的参数问题,需要的朋友可以参考下

在使用Process.Start 调用外部程序时,除了程序的地址之外,是可以传递参数的,Process.Start 也有多个重载;

//
    // 摘要:
    //   启动由包含进程启动信息(例如,要启动的进程的文件名)的参数指定的进程资源,并将该资源与新的 System.Diagnostics.Process
    //   组件关联。
    //
    // 参数:
    //  startInfo:
    //   System.Diagnostics.ProcessStartInfo,包含用于启动进程的信息(包括文件名和任何命令行参数)。
    //
    // 返回结果:
    //   与进程资源关联的新的 System.Diagnostics.Process 组件,或者如果没有启动进程资源(例如,如果重用了现有进程),则为 null。
    //
    // 异常:
    //  System.InvalidOperationException:
    //   在 startInfo 参数的 System.Diagnostics.ProcessStartInfo.FileName 属性中未指定任何文件名。-
    //   或 - startInfo 参数的 System.Diagnostics.ProcessStartInfo.UseShellExecute 属性为
    //   true,而 System.Diagnostics.ProcessStartInfo.RedirectStandardInput、System.Diagnostics.ProcessStartInfo.RedirectStandardOutput
    //   或 System.Diagnostics.ProcessStartInfo.RedirectStandardError 属性也为 true。- 或
    //   -startInfo 参数的 System.Diagnostics.ProcessStartInfo.UseShellExecute 属性为 true,而
    //   System.Diagnostics.ProcessStartInfo.UserName 属性不为 null 或空,或者 System.Diagnostics.ProcessStartInfo.Password
    //   属性不为 null。
    //
    //  System.ArgumentNullException:
    //   startInfo 参数为 null。
    //
    //  System.ComponentModel.Win32Exception:
    //   打开关联的文件时发生了错误。
    //
    //  System.ObjectDisposedException:
    //   该进程对象已被释放。
    public static Process Start(ProcessStartInfo startInfo);
    //
    // 摘要:
    //   通过指定文档或应用程序文件的名称来启动进程资源,并将资源与新的 System.Diagnostics.Process 组件关联。
    //
    // 参数:
    //  fileName:
    //   要在进程中运行的文档或应用程序文件的名称。
    //
    // 返回结果:
    //   与进程资源关联的新的 System.Diagnostics.Process 组件,或者如果没有启动进程资源(例如,如果重用了现有进程),则为 null。
    //
    // 异常:
    //  System.ComponentModel.Win32Exception:
    //   打开关联的文件时发生了错误。
    //
    //  System.ObjectDisposedException:
    //   该进程对象已被释放。
    //
    //  System.IO.FileNotFoundException:
    //   PATH 环境变量有包含引号的字符串。
    public static Process Start(string fileName);
    //
    // 摘要:
    //   通过指定应用程序的名称和一组命令行参数来启动一个进程资源,并将该资源与新的 System.Diagnostics.Process 组件相关联。
    //
    // 参数:
    //  fileName:
    //   要在该进程中运行的应用程序文件的名称。
    //
    //  arguments:
    //   启动该进程时传递的命令行参数。
    //
    // 返回结果:
    //   与该进程关联的新的 System.Diagnostics.Process 组件,或者如果没有启动进程资源(例如,如果重用了现有进程),则为 null。
    //
    // 异常:
    //  System.InvalidOperationException:
    //   fileName 或 arguments 参数为 null。
    //
    //  System.ComponentModel.Win32Exception:
    //   打开关联的文件时发生了错误。
    //
    //  System.ObjectDisposedException:
    //   该进程对象已被释放。
    //
    //  System.IO.FileNotFoundException:
    //   PATH 环境变量有包含引号的字符串。
    public static Process Start(string fileName, string arguments);
    //
    // 摘要:
    //   通过指定应用程序的名称、用户名、密码和域来启动一个进程资源,并将该资源与新的 System.Diagnostics.Process 组件关联起来。
    //
    // 参数:
    //  fileName:
    //   要在该进程中运行的应用程序文件的名称。
    //
    //  userName:
    //   启动进程时使用的用户名。
    //
    //  password:
    //   一个 System.Security.SecureString,它包含启动进程时要使用的密码。
    //
    //  domain:
    //   启动进程时要使用的域。
    //
    // 返回结果:
    //   与进程资源关联的新的 System.Diagnostics.Process 组件,或者如果没有启动进程资源(例如,如果重用了现有进程),则为 null。
    //
    // 异常:
    //  System.InvalidOperationException:
    //   未指定文件名。
    //
    //  System.ComponentModel.Win32Exception:
    //   fileName 不是可执行 (.exe) 文件。
    //
    //  System.ComponentModel.Win32Exception:
    //   打开关联的文件时发生了错误。
    //
    //  System.ObjectDisposedException:
    //   该进程对象已被释放。
    public static Process Start(string fileName, string userName, SecureString password, string domain);
    //
    // 摘要:
    //   通过指定应用程序的名称、一组命令行参数、用户名、密码和域来启动一个进程资源,并将该资源与新的 System.Diagnostics.Process
    //   组件关联起来。
    //
    // 参数:
    //  fileName:
    //   要在该进程中运行的应用程序文件的名称。
    //
    //  arguments:
    //   启动该进程时传递的命令行参数。
    //
    //  userName:
    //   启动进程时要使用的用户名。
    //
    //  password:
    //   一个 System.Security.SecureString,它包含启动进程时要使用的密码。
    //
    //  domain:
    //   启动进程时要使用的域。
    //
    // 返回结果:
    //   与进程资源关联的新的 System.Diagnostics.Process 组件,或者如果没有启动进程资源(例如,如果重用了现有进程),则为 null。
    //
    // 异常:
    //  System.InvalidOperationException:
    //   未指定文件名。
    //
    //  System.ComponentModel.Win32Exception:
    //   fileName 不是可执行 (.exe) 文件。
    //
    //  System.ComponentModel.Win32Exception:
    //   打开关联的文件时发生了错误。
    //
    //  System.ObjectDisposedException:
    //   该进程对象已被释放。
    public static Process Start(string fileName, string arguments, string userName, SecureString password, string domain);

其中的arguments 参数, 是有个空格的问题的, 在外部程序接收参数的 (Winform) 是用过Main(string[] args) 。 其中args是数组 , 在StartInfo.Arguments中的参数的间隔是根据空格进行分断的。 所以如果在传递的参数中是空格的,就需要 在  参数前后追加 “\"”  即:

string argument1 = "\"" + argv1 + "\""; 
  string argument2 = "\"" + argv2 + "\""; 
  Process process = new Process(); 
  process.StartInfo.FileName = System.Environment.CurrentDirectory + "//test.exe"; 
  process.StartInfo.Arguments = argument1 + " " + argument2; 
  process.StartInfo.UseShellExecute = true; ; 
  //启动 
  process.Start();

ok, 这样就能解决Process 传递参数的有空格的问题了。

以上所述是小编给大家介绍的C# 使用Process调用外部程序中所遇到的参数问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

您可能感兴趣的文章:
阅读全文