C # process parameters passed between programs, and Args receives parameters,

Source: Internet
Author: User

C # process parameters passed between programs, and Args receives parameters,

Memo, or leave it to those who are not in touch but need to know.

C # Passing parameters in a process is quite a lot of methods, but it is not that convenient to passing parameters between programs (of course, socket, Memory sharing and other methods belong to another type, which is not discussed here ), there will be a parameter Arg or Args at the entrance of the Main function, such as C, C #, or Java, which is provided to facilitate the input of parameters when it is called by a third-party software, it is commonly known as the startup parameter.

Recently, when using it, I need to input a path, and there are some symbols such as spaces in the path. The receiver always accepts errors. What should I do? Generally, input a string when passing parameters. The receiver can use string arg or string [] args. Therefore, the string type parameter is separated by space by default. Therefore, if the input parameter "D :\\ Program files \ Baidu Yun \ Auto Update \" is "D: \ Program", "files \ Baidu ", "Yun \ Auto", "Update \", what should I do? You can use double quotation marks ("") to enclose a parameter. In this case, you must note that the parameter end has the question.

  

 

 

The following example is passed (all in C # And no C/java program is called for test ):

Caller program:

Using System; using System. collections. generic; using System. linq; using System. text; using System. diagnostics; namespace argTest1 {class Program {static void Main (string [] args) {Console. writeLine ("Begin test"); Process p = new Process (); p. startInfo. fileName = ". \ argTest.exe "; p. startInfo. useShellExecute = true; ArgsBuilder arg = new ArgsBuilder (); arg. add ("hello"); arg. add ("D :\\ gadgets \ Assistant.exe"); arg. add (AppDomain. currentDomain. baseDirectory); arg. add ("D :\\ Program files \ Baidu Yun \ Auto Update \"); arg. add ("D:/Program files/Baidu Yun/Auto Update/"); arg. add ("Arg Ends"); p. startInfo. arguments = arg. toString (); p. start (); Console. readLine ();}
/// <Summary>
/// Parameter construction class
/// </Summary> public class ArgsBuilder {StringBuilder _ arg = new StringBuilder (); public ArgsBuilder () {_ arg. append ("");} /// <summary> /// Add the parameter /// </summary> /// <param name = "str"> </param> public void Add (string str) {if (str. endsWith ("\") // if the final processing is "\", it is converted to "\" and then to an escape character. {Str + = "\\";}
_ Arg. appendFormat ("\" {0} \ "", str); _ arg. append (""); // parameter delimiter} public override string ToString () {return _ arg. toString ();}}}}

Called:

Using System; using System. collections. generic; using System. linq; using System. text; using System. IO; namespace argTest {class Program {static void Main (string [] args) {if (args! = Null & args. length> 0) // print out the parameter {foreach (var item in args) {Console. writeLine (item) ;}} if (File. exists (args [1]) {System. diagnostics. process. start (args [1]);} string s = Console. readLine ();}}}

Result:

 

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.