C ා parameter transfer between programs and Args receiving parameter processing

Make a note, or leave it to someone you haven't touched but need to know.

C ා there are quite a lot of parameter methods in a process, but it is not so convenient to transfer parameters between programs (of course, socket, memory sharing and other methods belong to another category, which will not be discussed here). Our C or C 񖓿 or Java and so on will have a parameter Arg or Args at the function entry of Main, which is reserved for the convenience of being called by the third-party software When the parameter is passed in, it is also known as the start parameter.

Recently, when using, I need to pass in a path, and there are some symbols such as spaces in the path. The receiver always accepts the error. How to deal with this? Generally, a string is entered when transferring parameters, and the receiver can use string arg or string[] args. Therefore, the default is to use spaces to divide the parameters of string type as string []. So if you pass in a parameter like "D:\Program files\Baidu Yun\Auto Update \ \" and it will become "D:\Program", "files\Baidu", "Yun\Auto", "Update \", what should you do? You can use double quotation marks ("") to enclose a parameter. You need to pay attention to the problem of "\" at the end of the parameter.

  

 

 

The following examples are verified (all in C, no test calls C/java program 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:\\Gadget\\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 parameter /// </summary> /// <param name="str"></param> public void Add(string str) { if ( str.EndsWith ( "\\" ) ) //Process last if“\\",Will be translated into“\",Then it becomes an escape character. { str += "\\"; }
_arg .AppendFormat(
"\"{0}\"",str ); _arg.Append ( " "); //Parameter division symbol } public override string ToString() { return _arg.ToString(); } } } }

Callee:

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 parameters
            {
                foreach ( var item in args )
                {
                    Console.WriteLine (item );
                }
            }
            if(File.Exists(args[1])) 
            {
                System.Diagnostics.Process.Start ( args[1]);
            }

            string s=Console.ReadLine ( );
        }
    }
}

Result:

Tags: C# Java socket

Posted on Sun, 05 Apr 2020 12:49:15 -0700 by daniel244rock