這是我在 powershell 中作業的代碼,但要將其放在 C# 中,我必須將 "$string" 替換為 "$string2"; 但它不起作用(:它必須是“$string”才能作業......我嘗試了'$string'和($string)等等,但只是這個“$string”作業。幫助?
Powershell Code Working 100% :
$string2 = C:/Users/Sycho/Desktop/youtube-dl.exe -F https://youtu.be/f1A7SdVTlok | Out-String -stream | Select-String 133 ; "$string2".SubString(105)
var mainForm = Application.OpenForms.OfType<Form1>().Single();
string urlBoxText3 = mainForm.Controls["URL_BOX"].Text;
ProcessStartInfo startInfo6 = new ProcessStartInfo();
startInfo6.FileName = @"powershell.exe";
startInfo6.Arguments = "$string2 = C:/Users/Sycho/Desktop/youtube-dl.exe -F " urlBoxText3 "| Out-String -stream | Select-String 133 ; \"$string2\".SubString(105)";
startInfo6.RedirectStandardOutput = true;
startInfo6.RedirectStandardError = true;
startInfo6.UseShellExecute = false;
startInfo6.CreateNoWindow = true;
startInfo6.Verb = "runas";
Process process6 = new Process();
process6.StartInfo = startInfo6;
process6.Start();
string output = process6.StandardOutput.ReadToEnd();
label4.Text = output;
uj5u.com熱心網友回復:
你說PowerShell最終必須看到
"$string2".SubString(105)你的命令才能作業。當您通過其CLI呼叫 PowerShell 并通過(位置隱含的) /引數
powershell.exe向其傳遞命令時,您必須轉義字符,以便 PowerShell 將它們保留為在命令列決議后執行的命令的一部分.-Command-c"\"- 此答案中對此進行了更詳細的解釋。
因此,替換\"$string2\"為\\\"$string2\\\"(原文如此)。
$string2退后一步:您可以按如下方式簡化代碼,這樣就不需要封閉"..."
startInfo6.Arguments = "$string2 = (C:/Users/Sycho/Desktop/youtube-dl.exe -F " urlBoxText3 " | Select-String 133).Line; $string2.SubString(105)";
通過訪問呼叫
.Line發出的輸出物件的屬性,直接回傳Select-String一個字串- 匹配行 - (而不是默認發出的 match-info 物件)。Out-String -Stream已洗掉,因為它是不必要的:PowerShell在呼叫外部程式(例如.youtube-dl.exe
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/434578.html
上一篇:Get-AzureBlobStorage的糾纏單元測驗
下一篇:無法將IIS結果匯出到CSV
