簡單介紹C#使用Process呼叫外部程式中所遇到的參數問題的程式碼範例

黄舟
發布: 2017-03-17 11:16:41
原創
2289 人瀏覽過

這篇文章主要介紹了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呼叫外部程式中所遇到的參數問題的程式碼範例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!