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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

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

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

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

3 回答

?
哈士奇WWW

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超6個(gè)贊

我也遇到這個(gè)問(wèn)題。我沒(méi)有解析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);



查看完整回答
反對(duì) 回復(fù) 2019-12-14
  • 3 回答
  • 0 關(guān)注
  • 283 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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