A brief introduction to C # code Examples of parameter problems encountered in calling external programs using process

Source: Internet
Author: User
This article mainly describes the C # use of process calls external programs encountered in the parameter problems, the need for friends can refer to the following

When calling an external program using Process.Start, it is possible to pass parameters in addition to the address of the program, and Process.Start has multiple overloads;

Summary://Starts the process resource specified by the parameter that contains the process startup information (for example, the file name of the process to be started) and associates the resource with the new System.Diagnostics.Process//component.    Parameters://StartInfo://System.Diagnostics.ProcessStartInfo, containing information (including file name and any command-line arguments) used to start the process.    Returns the result://The new System.Diagnostics.Process component associated with the process resource, or null if no process resource was started (for example, if an existing process was reused).  Exception://System.InvalidOperationException://System.Diagnostics.ProcessStartInfo.FileName in StartInfo parameter No file name is specified in the property. -//or-the System.Diagnostics.ProcessStartInfo.UseShellExecute property of the StartInfo parameter is/true, and System.Diagnostics.Proc Essstartinfo.redirectstandardinput, System.Diagnostics.ProcessStartInfo.RedirectStandardOutput//or The System.Diagnostics.ProcessStartInfo.RedirectStandardError property is also true. -The System.Diagnostics.ProcessStartInfo.UseShellExecute property of the///-startinfo parameter is true, while//System.Diagnostics.Proce    The Ssstartinfo.username property is not null or empty, or the System.Diagnostics.ProcessStartInfo.Password//property is not NULL. System.argumentNullexception://The StartInfo parameter is null.    System.ComponentModel.Win32Exception://An error occurred while opening the associated file.    System.objectdisposedexception://The Process object has been disposed.    public static Process Start (ProcessStartInfo startinfo);    Summary://The process resource is started by specifying the name of the document or application file, and the resource is associated with the new System.Diagnostics.Process component.    Parameters://FileName://The name of the document or application file to run in the process.    Returns the result://The new System.Diagnostics.Process component associated with the process resource, or null if no process resource was started (for example, if an existing process was reused).    Exception://System.ComponentModel.Win32Exception://An error occurred while opening the associated file.    System.objectdisposedexception://The Process object has been disposed.    System.IO.FileNotFoundException://The PATH environment variable has a string containing quotation marks.    public static Process Start (string fileName);    Summary://Starts a process resource by specifying the name of the application and a set of command-line parameters, and associates the resource with the new System.Diagnostics.Process component.    Parameters://FileName://The name of the application file to run in the process.    Arguments://Command-line arguments that are passed when the process is started. Return Result://With theThe new System.Diagnostics.Process component that is associated with the process, or null if the process resource is not started (for example, if an existing process is reused).    Exception://System.InvalidOperationException://FileName or arguments parameter is null.    System.ComponentModel.Win32Exception://An error occurred while opening the associated file.    System.objectdisposedexception://The Process object has been disposed.    System.IO.FileNotFoundException://The PATH environment variable has a string containing quotation marks.    public static Process Start (string fileName, string arguments);    Summary: Start a process resource by specifying the application's name, user name, password, and domain, and associate the resource with the new System.Diagnostics.Process component.    Parameters://FileName://The name of the application file to run in the process.    UserName://The user name to use when starting the process.    Password://A System.Security.SecureString that contains the password to be used when the process is started.    Domain://The field to use when starting the process.    Returns the result://The new System.Diagnostics.Process component associated with the process resource, or null if no process resource was started (for example, if an existing process was reused).    Exception://System.InvalidOperationException://File name not specified.  System.ComponentModel.Win32Exception:  FileName is not an executable (. exe) file.    System.ComponentModel.Win32Exception://An error occurred while opening the associated file.    System.objectdisposedexception://The Process object has been disposed.    public static Process Start (String fileName, String userName, SecureString password, string domain);    Summary: Start a process resource by specifying the name of the application, a set of command-line arguments, a user name, a password, and a domain, and associate the resource with the new System.Diagnostics.Process//component.    Parameters://FileName://The name of the application file to run in the process.    Arguments://Command-line arguments that are passed when the process is started.    UserName://The user name to use when starting the process.    Password://A System.Security.SecureString that contains the password to be used when the process is started.    Domain://The field to use when starting the process.    Returns the result://The new System.Diagnostics.Process component associated with the process resource, or null if no process resource was started (for example, if an existing process was reused).    Exception://System.InvalidOperationException://File name not specified.    System.ComponentModel.Win32Exception://FileName is not an executable (. exe) file.   System.ComponentModel.Win32Exception://An error occurred while opening the associated file. System.objectdisposedexception://The Process object has been disposed. public static Process Start (string fileName, String arguments, string userName, SecureString password, string domain);

Where the arguments parameter, there is a space problem, the external program receives the parameter (Winform) is used over main (string[] args). Where args is an array, the interval of the arguments in the startinfo.arguments is broken according to the space. So if a space is in the passed argument, you need to append "\" to the parameter before and after it:

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;;   Start   process. Start ();

OK, this will solve the problem of having spaces in the process pass parameter.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.