Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将Powershell命令输出保存到listbox_C#_Powershell_User Interface_Listbox - Fatal编程技术网

C# 将Powershell命令输出保存到listbox

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

正在尝试使用c#编写一些GUI程序

我需要执行一些Powershell命令(比如GetContentFilterphrase)并将它们的输出添加到列表框中。 我现在能得到的一切只是列表框第一行中的一个集合

谁能描述一下如何正确地做到这一点

以下是我的一个尝试:

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);}
似乎没有帮助