Analysis on Parameter Problems in Calling External Programs by C Using Process

  • 2021-12-09 09:47:07
  • OfStack

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


//
    //  Summary :
    //    Start the process resource specified by a parameter that contains process startup information (for example, the file name of the process to start), and associate the resource with the new  System.Diagnostics.Process
    //    Component association. 
    //
    //  Parameter :
    //  startInfo:
    //   System.Diagnostics.ProcessStartInfo Contains information for starting the process, including the file name and any command-line arguments. 
    //
    //  Return results :
    //    The new associated with the process resource  System.Diagnostics.Process  Component, or if there is no startup process resource (for example, if an existing process is reused); Otherwise,  null . 
    //
    //  Anomaly :
    //  System.InvalidOperationException:
    //    In  startInfo  Parameter  System.Diagnostics.ProcessStartInfo.FileName  Property does not specify any file name. -
    //    Or  - startInfo  Parameter  System.Diagnostics.ProcessStartInfo.UseShellExecute  Property is 
    //   true , and  System.Diagnostics.ProcessStartInfo.RedirectStandardInput , System.Diagnostics.ProcessStartInfo.RedirectStandardOutput
    //    Or  System.Diagnostics.ProcessStartInfo.RedirectStandardError  Property is also  true . -  Or 
    //   -startInfo  Parameter  System.Diagnostics.ProcessStartInfo.UseShellExecute  Property is  true , and 
    //   System.Diagnostics.ProcessStartInfo.UserName  Property is not  null  Or empty, or  System.Diagnostics.ProcessStartInfo.Password
    //    Property is not  null . 
    //
    //  System.ArgumentNullException:
    //   startInfo  Parameter is  null . 
    //
    //  System.ComponentModel.Win32Exception:
    //    An error occurred opening the associated file. 
    //
    //  System.ObjectDisposedException:
    //    The process object has been released. 
    public static Process Start(ProcessStartInfo startInfo);
    //
    //  Summary :
    //    Start the process resource by specifying the name of the document or application file, and associate the resource with the new  System.Diagnostics.Process  Component association. 
    //
    //  Parameter :
    //  fileName:
    //    The name of the document or application file to run in the process. 
    //
    //  Return results :
    //    The new associated with the process resource  System.Diagnostics.Process  Component, or if there is no startup process resource (for example, if an existing process is reused); Otherwise,  null . 
    //
    //  Anomaly :
    //  System.ComponentModel.Win32Exception:
    //    An error occurred opening the associated file. 
    //
    //  System.ObjectDisposedException:
    //    The process object has been released. 
    //
    //  System.IO.FileNotFoundException:
    //   PATH  Environment variables have strings containing quotation marks. 
    public static Process Start(string fileName);
    //
    //  Summary :
    //    By specifying the name of the application and the 1 Group command line arguments to start 1 Process resources, and associate this resource with the new  System.Diagnostics.Process  Component is associated with the. 
    //
    //  Parameter :
    //  fileName:
    //    The name of the application file to run in this process. 
    //
    //  arguments:
    //    Command line arguments passed when starting the process. 
    //
    //  Return results :
    //    The new associated with the process  System.Diagnostics.Process  Component, or if there is no startup process resource (for example, if an existing process is reused); Otherwise,  null . 
    //
    //  Anomaly :
    //  System.InvalidOperationException:
    //   fileName  Or  arguments  Parameter is  null . 
    //
    //  System.ComponentModel.Win32Exception:
    //    An error occurred opening the associated file. 
    //
    //  System.ObjectDisposedException:
    //    The process object has been released. 
    //
    //  System.IO.FileNotFoundException:
    //   PATH  Environment variables have strings containing quotation marks. 
    public static Process Start(string fileName, string arguments);
    //
    //  Summary :
    //    Start by specifying the name, user name, password, and domain of the application 1 Process resources, and associate this resource with the new  System.Diagnostics.Process  Components are associated. 
    //
    //  Parameter :
    //  fileName:
    //    The name of the application file to run in this process. 
    //
    //  userName:
    //    The user name to use when starting the process. 
    //
    //  password:
    //   1 A  System.Security.SecureString That contains the password to use when starting the process. 
    //
    //  domain:
    //    The domain to use when starting the process. 
    //
    //  Return results :
    //    The new associated with the process resource  System.Diagnostics.Process  Component, or if there is no startup process resource (for example, if an existing process is reused); Otherwise,  null . 
    //
    //  Anomaly :
    //  System.InvalidOperationException:
    //    File name is not specified. 
    //
    //  System.ComponentModel.Win32Exception:
    //   fileName  Not executable  (.exe)  Files. 
    //
    //  System.ComponentModel.Win32Exception:
    //    An error occurred opening the associated file. 
    //
    //  System.ObjectDisposedException:
    //    The process object has been released. 
    public static Process Start(string fileName, string userName, SecureString password, string domain);
    //
    //  Summary :
    //    By specifying the name of the application, 1 Group command line parameters, user name, password, and domain to start 1 Process resources, and associate this resource with the new  System.Diagnostics.Process
    //    Components are associated. 
    //
    //  Parameter :
    //  fileName:
    //    The name of the application file to run in this process. 
    //
    //  arguments:
    //    Command line arguments passed when starting the process. 
    //
    //  userName:
    //    The user name to use when starting the process. 
    //
    //  password:
    //   1 A  System.Security.SecureString That contains the password to use when starting the process. 
    //
    //  domain:
    //    The domain to use when starting the process. 
    //
    //  Return results :
    //    The new associated with the process resource  System.Diagnostics.Process  Component, or if there is no startup process resource (for example, if an existing process is reused); Otherwise,  null . 
    //
    //  Anomaly :
    //  System.InvalidOperationException:
    //    File name is not specified. 
    //
    //  System.ComponentModel.Win32Exception:
    //   fileName  Not executable  (.exe)  Files. 
    //
    //  System.ComponentModel.Win32Exception:
    //    An error occurred opening the associated file. 
    //
    //  System.ObjectDisposedException:
    //    The process object has been released. 
    public static Process Start(string fileName, string arguments, string userName, SecureString password, string domain);

Among them, arguments parameter has a space problem, and Main (string [] args) is used to receive parameters in external programs. Where args is an array, the parameters in StartInfo. Arguments are separated by spaces. Therefore, if there is a space in the passed parameter, you need to append "\" "before and after the parameter, 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; ; 
  // Start  
  process.Start();

ok, this can solve the problem of Process passing parameters with spaces.


Related articles: