zoukankan      html  css  js  c++  java
  • C# 使用Process调用外部程序中所遇到的参数问题

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

      1         //
      2         // 摘要:
      3         //     启动由包含进程启动信息(例如,要启动的进程的文件名)的参数指定的进程资源,并将该资源与新的 System.Diagnostics.Process
      4         //     组件关联。
      5         //
      6         // 参数:
      7         //   startInfo:
      8         //     System.Diagnostics.ProcessStartInfo,包含用于启动进程的信息(包括文件名和任何命令行参数)。
      9         //
     10         // 返回结果:
     11         //     与进程资源关联的新的 System.Diagnostics.Process 组件,或者如果没有启动进程资源(例如,如果重用了现有进程),则为 null。
     12         //
     13         // 异常:
     14         //   System.InvalidOperationException:
     15         //     在 startInfo 参数的 System.Diagnostics.ProcessStartInfo.FileName 属性中未指定任何文件名。-
     16         //     或 - startInfo 参数的 System.Diagnostics.ProcessStartInfo.UseShellExecute 属性为
     17         //     true,而 System.Diagnostics.ProcessStartInfo.RedirectStandardInput、System.Diagnostics.ProcessStartInfo.RedirectStandardOutput
     18         //     或 System.Diagnostics.ProcessStartInfo.RedirectStandardError 属性也为 true。- 或
     19         //     -startInfo 参数的 System.Diagnostics.ProcessStartInfo.UseShellExecute 属性为 true,而
     20         //     System.Diagnostics.ProcessStartInfo.UserName 属性不为 null 或空,或者 System.Diagnostics.ProcessStartInfo.Password
     21         //     属性不为 null。
     22         //
     23         //   System.ArgumentNullException:
     24         //     startInfo 参数为 null。
     25         //
     26         //   System.ComponentModel.Win32Exception:
     27         //     打开关联的文件时发生了错误。
     28         //
     29         //   System.ObjectDisposedException:
     30         //     该进程对象已被释放。
     31         public static Process Start(ProcessStartInfo startInfo);
     32         //
     33         // 摘要:
     34         //     通过指定文档或应用程序文件的名称来启动进程资源,并将资源与新的 System.Diagnostics.Process 组件关联。
     35         //
     36         // 参数:
     37         //   fileName:
     38         //     要在进程中运行的文档或应用程序文件的名称。
     39         //
     40         // 返回结果:
     41         //     与进程资源关联的新的 System.Diagnostics.Process 组件,或者如果没有启动进程资源(例如,如果重用了现有进程),则为 null。
     42         //
     43         // 异常:
     44         //   System.ComponentModel.Win32Exception:
     45         //     打开关联的文件时发生了错误。
     46         //
     47         //   System.ObjectDisposedException:
     48         //     该进程对象已被释放。
     49         //
     50         //   System.IO.FileNotFoundException:
     51         //     PATH 环境变量有包含引号的字符串。
     52         public static Process Start(string fileName);
     53         //
     54         // 摘要:
     55         //     通过指定应用程序的名称和一组命令行参数来启动一个进程资源,并将该资源与新的 System.Diagnostics.Process 组件相关联。
     56         //
     57         // 参数:
     58         //   fileName:
     59         //     要在该进程中运行的应用程序文件的名称。
     60         //
     61         //   arguments:
     62         //     启动该进程时传递的命令行参数。
     63         //
     64         // 返回结果:
     65         //     与该进程关联的新的 System.Diagnostics.Process 组件,或者如果没有启动进程资源(例如,如果重用了现有进程),则为 null。
     66         //
     67         // 异常:
     68         //   System.InvalidOperationException:
     69         //     fileName 或 arguments 参数为 null。
     70         //
     71         //   System.ComponentModel.Win32Exception:
     72         //     打开关联的文件时发生了错误。
     73         //
     74         //   System.ObjectDisposedException:
     75         //     该进程对象已被释放。
     76         //
     77         //   System.IO.FileNotFoundException:
     78         //     PATH 环境变量有包含引号的字符串。
     79         public static Process Start(string fileName, string arguments);
     80         //
     81         // 摘要:
     82         //     通过指定应用程序的名称、用户名、密码和域来启动一个进程资源,并将该资源与新的 System.Diagnostics.Process 组件关联起来。
     83         //
     84         // 参数:
     85         //   fileName:
     86         //     要在该进程中运行的应用程序文件的名称。
     87         //
     88         //   userName:
     89         //     启动进程时使用的用户名。
     90         //
     91         //   password:
     92         //     一个 System.Security.SecureString,它包含启动进程时要使用的密码。
     93         //
     94         //   domain:
     95         //     启动进程时要使用的域。
     96         //
     97         // 返回结果:
     98         //     与进程资源关联的新的 System.Diagnostics.Process 组件,或者如果没有启动进程资源(例如,如果重用了现有进程),则为 null。
     99         //
    100         // 异常:
    101         //   System.InvalidOperationException:
    102         //     未指定文件名。
    103         //
    104         //   System.ComponentModel.Win32Exception:
    105         //     fileName 不是可执行 (.exe) 文件。
    106         //
    107         //   System.ComponentModel.Win32Exception:
    108         //     打开关联的文件时发生了错误。
    109         //
    110         //   System.ObjectDisposedException:
    111         //     该进程对象已被释放。
    112         public static Process Start(string fileName, string userName, SecureString password, string domain);
    113         //
    114         // 摘要:
    115         //     通过指定应用程序的名称、一组命令行参数、用户名、密码和域来启动一个进程资源,并将该资源与新的 System.Diagnostics.Process
    116         //     组件关联起来。
    117         //
    118         // 参数:
    119         //   fileName:
    120         //     要在该进程中运行的应用程序文件的名称。
    121         //
    122         //   arguments:
    123         //     启动该进程时传递的命令行参数。
    124         //
    125         //   userName:
    126         //     启动进程时要使用的用户名。
    127         //
    128         //   password:
    129         //     一个 System.Security.SecureString,它包含启动进程时要使用的密码。
    130         //
    131         //   domain:
    132         //     启动进程时要使用的域。
    133         //
    134         // 返回结果:
    135         //     与进程资源关联的新的 System.Diagnostics.Process 组件,或者如果没有启动进程资源(例如,如果重用了现有进程),则为 null。
    136         //
    137         // 异常:
    138         //   System.InvalidOperationException:
    139         //     未指定文件名。
    140         //
    141         //   System.ComponentModel.Win32Exception:
    142         //     fileName 不是可执行 (.exe) 文件。
    143         //
    144         //   System.ComponentModel.Win32Exception:
    145         //     打开关联的文件时发生了错误。
    146         //
    147         //   System.ObjectDisposedException:
    148         //     该进程对象已被释放。
    149         public static Process Start(string fileName, string arguments, string userName, SecureString password, string domain);
    View Code

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

    1     string argument1 = """ + argv1 + """;  
    2     string argument2 = """ + argv2 + """;  
    3     Process process = new Process();  
    4     process.StartInfo.FileName = System.Environment.CurrentDirectory + "//test.exe";  
    5     process.StartInfo.Arguments = argument1 + " " + argument2;  
    6     process.StartInfo.UseShellExecute = true; ;  
    7     //启动  
    8     process.Start();  

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

  • 相关阅读:
    EF使用CodeFirst创建数据库和表
    EF使用CodeFirst创建数据库和表
    MVC实现实现文件流打包成压缩包
    MVC实现实现文件流打包成压缩包
    MVC实现实现文件流打包成压缩包
    Visual Studio Code 开发 .NET Core 看这篇就够了
    Visual Studio Code 开发 .NET Core 看这篇就够了
    P1438 无聊的数列
    P3380 【模板】二逼平衡树(树套树)
    SP16580 QTREE7
  • 原文地址:https://www.cnblogs.com/chongde/p/6564048.html
Copyright © 2011-2022 走看看