gpt4 book ai didi

C#,如何在 foreach 循环期间将字符串拆分为新行?

转载 作者:行者123 更新时间:2023-11-30 22:11:59 25 4
gpt4 key购买 nike

使用 Powershell 对象并运行 foreach 循环来获取结果,

我得到了 2 个正确的 IP 地址结果 144.68.205.19 和 144.68.205.22 但它打印在 1 行中,

144.68.205.19144.68.205.22

应该像这样拆分新行,

144.68.205.19
144.68.205.22

请指教,这里是C#代码,

// Powershell
Runspace runSpace = RunspaceFactory.CreateRunspace();
runSpace.Open();
Pipeline pipeline = runSpace.CreatePipeline();

Command invokeScript = new Command("Invoke-Command");
RunspaceInvoke invoke = new RunspaceInvoke();

// invoke-command -computername compName -scriptblock { get-process }

ScriptBlock sb = invoke.Invoke("{"+ PowerShellCodeBox.Text +"}")[0].BaseObject as ScriptBlock;
invokeScript.Parameters.Add("scriptBlock", sb);
invokeScript.Parameters.Add("computername", TextBoxServer.Text);

string str = "";

pipeline.Commands.Add(invokeScript);
Collection<PSObject> output = pipeline.Invoke();
foreach (PSObject psObject in output)
{

str = str + psObject;
}

if (str == ""){
str = "Error";

ResultBox.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FF0000");
}

ResultBox.Text = str;

最佳答案

您可以在构建 str 时插入一个新行:

foreach (PSObject psObject in output)
{
str += "\n" + psObject;
}

或者使用string.Join

string str = String.Join("\n", output);

关于C#,如何在 foreach 循环期间将字符串拆分为新行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19940158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com