当前位置:网站首页>C # startup program loses double quotation marks for parameters passed. How to solve it?

C # startup program loses double quotation marks for parameters passed. How to solve it?

2022-06-25 08:50:00 Sir, silence first

c# Parameters passed after starting the program do not have double quotes

bug Exhibition

 Insert picture description here  Insert picture description here

C# Program start code

        static void Main(string[] args)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();// Create a parameter transfer object 
            dic.Add("dst", "ddd");
            dic.Add("dasdst", "ddd");
            var requesDataStr = JsonConvert.SerializeObject(dic);// Serializing objects , For parameter passing 

            ProcessStartInfo startInfo = new ProcessStartInfo();// Create a startup program ProcessStartInfo
            startInfo.FileName = "MD5Encryption.exe"; // The name of the launched Application 

            startInfo.Arguments = @requesDataStr;// Parameters passed to the starting program 
            Process.Start(startInfo);// Start the program 

            Console.WriteLine(" Program started ");
            Console.ReadKey();
        }
		

C# The code of the program being started

        static void Main(string[] args)
        {
            Console.WriteLine(args[0]);// Just print the passed string 
            Console.ReadKey();
            Console.ReadKey();

		}

The effect is the picture above

Solution

Escape the double quotation marks inside

Code in 23 That's ok

 Insert picture description here

            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("dst", "ddd");
            dic.Add("dasdst", "ddd");
            var requesDataStr = JsonConvert.SerializeObject(dic);

            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "MD5Encryption.exe"; // The name of the launched Application 

            requesDataStr = requesDataStr.Replace("\"","\\\"");
            
            startInfo.Arguments = @requesDataStr;
            Process.Start(startInfo);

            Console.WriteLine(" Program started ");
            Console.ReadKey();

Replace the string with another string

Code No. 24 That's ok
 Insert picture description here String substitution

            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("dst", "ddd");
            dic.Add("dasdst", "ddd");
            var requesDataStr = JsonConvert.SerializeObject(dic);

            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "MD5Encryption.exe"; // The name of the launched Application 
            
            requesDataStr = requesDataStr.Replace("\"", "#");

            startInfo.Arguments = @requesDataStr;
            Process.Start(startInfo);

            Console.WriteLine(" Program started ");
            Console.ReadKey();

The string is replaced again

        static void Main(string[] args)
        {
            args[0] = args[0].Replace("#", "\"");
            Console.WriteLine(args[0]);
            }

Modify the encoding format of the string

I won't , This method is not available for the time being .

No comments or personal letters

 Insert picture description here

.

原网站

版权声明
本文为[Sir, silence first]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250801478485.html