C# 如何从通过Process.Start()作为列表或数组运行的命令中读取标准输出
我需要将某台主机上运行的所有计划任务的列表放入C#中的列表或数组中 询问C# 如何从通过Process.Start()作为列表或数组运行的命令中读取标准输出,c#,C#,我需要将某台主机上运行的所有计划任务的列表放入C#中的列表或数组中 询问 schtasks /query /S CHESTNUT105B /FO List 返回如下列表: HostName: CHESTNUT105B TaskName: Calculator Next Run Time: 12:00:00, 10/28/2010 Status: Running HostName: CHESTNUT105B TaskName: Google
schtasks /query /S CHESTNUT105B /FO List
返回如下列表:
HostName: CHESTNUT105B
TaskName: Calculator
Next Run Time: 12:00:00, 10/28/2010
Status: Running
HostName: CHESTNUT105B
TaskName: GoogleUpdateTaskMachineCore
Next Run Time: At logon time
Status:
HostName: CHESTNUT105B
TaskName: GoogleUpdateTaskMachineCore
Next Run Time: 13:02:00, 10/28/2010
我有以下代码来执行上面指定的命令:
static void Main(string[] args)
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "SCHTASKS.exe";
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
string MachineName = "CHESTNUT105b";
string ScheduledTaskName = "Calculator";
string activeDirectoryDomainName = "xxx";
string userName = "xxx";
string password = "xxxxx";
p.StartInfo.Arguments = String.Format("/Query /S {0} /FO LIST", MachineName);
p.Start();
}
如何读取在C#中生成的列表?这个问题的一部分可以用前面的“C#如何读取带参数的控制台输出”来回答 在按照问题中的建议将控制台输出检索到StreamReader中之后,您只需将控制台输出解析为单独的计划任务,然后再解析为存储您感兴趣的每段数据的对象
要分解为单个任务,您似乎只需使用:
str.split(“\n\n”)
-这会将每个任务作为一个单独的字符串提供给您,因此循环此数组,并创建一个类,该类读取字符串并通过解析数据填充其值。类似的方法应该可以工作(未测试)。这将在列表的一个元素中包含每一行输出
class GetSchTasks {
List<string> output = new List<string>();
public void Run()
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "SCHTASKS.exe";
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
string MachineName = "CHESTNUT105b";
string ScheduledTaskName = "Calculator";
string activeDirectoryDomainName = "xxx";
string userName = "xxx";
string password = "xxxxx";
p.StartInfo.Arguments = String.Format("/Query /S {0} /FO LIST", MachineName);
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
p.WaitForExit();
p.Close();
p.Dispose();
}
void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
//Handle errors here
}
void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
output.Add(e.Data);
}
}
类GetSchTasks{
列表输出=新列表();
公开募捐
{
过程p=新过程();
p、 StartInfo.UseShellExecute=false;
p、 StartInfo.FileName=“SCHTASKS.exe”;
p、 StartInfo.RedirectStandardError=true;
p、 StartInfo.RedirectStandardOutput=true;
p、 StartInfo.CreateNoWindow=true;
p、 StartInfo.WindowStyle=ProcessWindowStyle.Hidden;
字符串MachineName=“CHESTNUT105b”;
字符串ScheduledTaskName=“计算器”;
字符串activeDirectoryDomainName=“xxx”;
字符串userName=“xxx”;
字符串密码=“xxxxx”;
p、 StartInfo.Arguments=String.Format(“/Query/S{0}/FO LIST”,MachineName);
p、 Start();
p、 BeginOutputReadLine();
p、 BeginErrorReadLine();
p、 OutputDataReceived+=新的DataReceivedEventHandler(p_OutputDataReceived);
p、 ErrorDataReceived+=新的DataReceivedEventHandler(p_ErrorDataReceived);
p、 WaitForExit();
p、 Close();
p、 处置();
}
无效p_ErrorDataReceived(对象发送方,DataReceivedEventArgs e)
{
//在这里处理错误
}
无效p_OutputDataReceived(对象发送方,DataReceivedEventArgs e)
{
输出。添加(如数据);
}
}
现在,您可以在之后解释该列表,以构建一组合适的对象来表示每个计划任务,或者不表示,具体取决于实际用例。您还可以在
p\u OutputDataReceived
处理程序本身中构建一个ScheduledTask
s对象列表,只需将每一行与预期的开头进行比较,例如,if(e.Data.StartsWith(“HostName:”){//解析该行并获取主机名}
我确信有更好的方法来检索列表-通过WMI类,但我对该名称空间了解不够,无法提供答案。如果没有事件处理程序,是否有任何方法可以做到这一点?是的,将p.StandardOutput附加到StreamReader,请参阅问题@davisoa链接()