C# 将Powershell命令输出保存到listbox
正在尝试使用c#编写一些GUI程序 我需要执行一些Powershell命令(比如GetContentFilterphrase)并将它们的输出添加到列表框中。 我现在能得到的一切只是列表框第一行中的一个集合 谁能描述一下如何正确地做到这一点 以下是我的一个尝试:C# 将Powershell命令输出保存到listbox,c#,powershell,user-interface,listbox,C#,Powershell,User Interface,Listbox,正在尝试使用c#编写一些GUI程序 我需要执行一些Powershell命令(比如GetContentFilterphrase)并将它们的输出添加到列表框中。 我现在能得到的一切只是列表框第一行中的一个集合 谁能描述一下如何正确地做到这一点 以下是我的一个尝试: var rulelist = new List<string>(); var rsConfig = RunspaceConfiguration.Create(); using (var myRu
var rulelist = new List<string>();
var rsConfig = RunspaceConfiguration.Create();
using (var myRunSpace = RunspaceFactory.CreateRunspace(rsConfig))
{
PSSnapInException snapInException = null;
var info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out snapInException);
myRunSpace.Open();
using (var ps = PowerShell.Create())
{
ps.AddScript("Get-ContentFilterPhrase | Where-Object {$_.Influence -Match 'BadWord'}| Format-Table -Property Phrase");
var results = ps.Invoke();
foreach (var result in results)
{
rulelist.Add(result.ToString());
}
}
listBox1.Items.Add(rulelist);
}
var rulelist=新列表();
var rsConfig=RunspaceConfiguration.Create();
使用(var myRunSpace=RunspaceFactory.CreateRunspace(rsConfig))
{
PSSnapInException snapInException=null;
var info=rsConfig.AddPSSnapIn(“Microsoft.Exchange.Management.PowerShell.E2010”,out snapinception);
myRunSpace.Open();
使用(var ps=PowerShell.Create())
{
ps.AddScript(“获取ContentFilterPhrase | Where Object{$\影响-匹配'BadWord'}|格式表-属性短语”);
var results=ps.Invoke();
foreach(结果中的var结果)
{
添加(result.ToString());
}
}
listBox1.Items.Add(规则列表);
}
因为您添加的是一组字符串,而不是“a”字符串。。如果要添加它们,请添加每个?将整个集合添加到列表框中,使其显示为System.Net.Generics.Collections
(或类似的内容),您必须循环查看rulelist
中的每个项,并将其添加到列表框中,与您为每个项填充的rulelist
完全相同(规则列表中的var result){listBox1.Items.Add(result);}
似乎没有帮助,因为您添加的是一堆字符串,而不是“a”字符串..如果要添加它们,请添加每一个?将整个集合添加到列表框中,使其显示为System.Net.Generics.Collections
(或类似内容)您必须循环查看rulelist
中的每个项目,将其添加到列表框中,与您填充的rulelist
foreach(规则列表中的var result){listBox1.Items.Add(result);}
似乎没有帮助