第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

C#中的轉(zhuǎn)義命令行參數(shù)

C#中的轉(zhuǎn)義命令行參數(shù)

慕虎7371278 2019-10-28 18:48:30
簡潔版本:將參數(shù)用引號引起來并轉(zhuǎn)義\和是否足夠"?代碼版本我想string[] args使用ProcessInfo.Arguments 將命令行參數(shù)傳遞給另一個進程。ProcessStartInfo info = new ProcessStartInfo();info.FileName = Application.ExecutablePath;info.UseShellExecute = true;info.Verb = "runas"; // Provides Run as Administratorinfo.Arguments = EscapeCommandLineArguments(args);Process.Start(info);問題是我將參數(shù)作為數(shù)組獲取,必須將它們合并為單個字符串??梢栽O(shè)計一個參數(shù)來欺騙我的程序。my.exe "C:\Documents and Settings\MyPath \" --kill-all-humans \" except fry"根據(jù)這個答案,我創(chuàng)建了以下函數(shù)來轉(zhuǎn)義單個參數(shù),但是我可能錯過了一些東西。private static string EscapeCommandLineArguments(string[] args){    string arguments = "";    foreach (string arg in args)    {        arguments += " \"" +            arg.Replace ("\\", "\\\\").Replace("\"", "\\\"") +            "\"";    }    return arguments;}這足夠好還是有任何框架功能呢?
查看完整描述

3 回答

?
qq_遁去的一_1

TA貢獻1725條經(jīng)驗 獲得超8個贊

我也遇到這個問題。我沒有解析args,而是采用了完整的原始命令行并修剪了可執(zhí)行文件。即使不需要/未使用,這也具有在呼叫中保留空格的額外好處。它仍然必須在可執(zhí)行文件中追逐轉(zhuǎn)義符,但這似乎比args容易。


var commandLine = Environment.CommandLine;

var argumentsString = "";


if(args.Length > 0)

{

    // Re-escaping args to be the exact same as they were passed is hard and misses whitespace.

    // Use the original command line and trim off the executable to get the args.

    var argIndex = -1;

    if(commandLine[0] == '"')

    {

        //Double-quotes mean we need to dig to find the closing double-quote.

        var backslashPending = false;

        var secondDoublequoteIndex = -1;

        for(var i = 1; i < commandLine.Length; i++)

        {

            if(backslashPending)

            {

                backslashPending = false;

                continue;

            }

            if(commandLine[i] == '\\')

            {

                backslashPending = true;

                continue;

            }

            if(commandLine[i] == '"')

            {

                secondDoublequoteIndex = i + 1;

                break;

            }

        }

        argIndex = secondDoublequoteIndex;

    }

    else

    {

        // No double-quotes, so args begin after first whitespace.

        argIndex = commandLine.IndexOf(" ", System.StringComparison.Ordinal);

    }

    if(argIndex != -1)

    {

        argumentsString = commandLine.Substring(argIndex + 1);

    }

}


Console.WriteLine("argumentsString: " + argumentsString);


查看完整回答
反對 回復(fù) 2019-10-28
  • 3 回答
  • 0 關(guān)注
  • 752 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號