Home  >  Article  >  Backend Development  >  A brief introduction to the code examples of parameter problems encountered in C# using Process to call external programs.

A brief introduction to the code examples of parameter problems encountered in C# using Process to call external programs.

黄舟
黄舟Original
2017-03-17 11:16:412281browse

This article mainly introduces C# The parameter problems encountered when using Process to call external programs. Friends in need can refer to

When using Process.Start to call external programs In addition to the address of the program, parameters can be passed. Process.Start also has multiple overloads;

//
    // 摘要:
    //   启动由包含进程启动信息(例如,要启动的进程的文件名)的参数指定的进程资源,并将该资源与新的 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);

The arguments parameter has a space problem, and the parameters are received in an external program ( Winform) is using Main(string[] args). Among them, args is an array, and the intervals between parameters in StartInfo.Arguments are separated according to spaces. Therefore, if there are spaces in the passed parameters, you need to append "\"" before and after the parameters, that is:

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. This will solve the problem of spaces in the parameters passed by Process.

The above is the detailed content of A brief introduction to the code examples of parameter problems encountered in C# using Process to call external programs.. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn