Voraussetzung:C# Ausführen mehrerer Befehle in cmd.exe gleichzeitig und schreiben die Ausgabe in eine Textdatei
Ausführen mehrerer Befehle in cmd.exe gleichzeitig und die Ausgabe in eine Textdatei mit C# einschließlich der Startzeit schreiben und Endzeit des ausgeführten Befehls
Beachten Sie, dass ich die dot net Version 2.0 verwenden möchte, da dieser Code auf einem älteren Betriebssystem wie XP ausgeführt werden muss. Parallel und ConcurrentBag Funktion ist unter .net 4.5 Version
-Code Zusammenfassung:
- eine Stringliste erstellt, die ausgeführt werden alle Befehle enthält.
Iterieren durch die String-Liste und den Befehl als Parameter an eine Klasse übergeben, die eine statische Methode RunCommand (string command) machen
Verwendung Gewinde hat, dass jeder Befehl in einem separaten Thread parallel ausgeführt wird.
Problem:
Der Code läuft gut, aber die Ausgabe wird mit Renditen von allen Befehlen vermischt beim Schreiben zu trösten, und ich bin sicher, dass das gleiche Problem auftreten würde, wenn das Schreiben in Datei! Wie stelle ich sicher, dass alle Befehle parallel laufen + Ausgabe sieht ordentlich und nicht durcheinander. Beachten Sie auch, dass beim Versuch, in eine Datei zu schreiben, ein Fehler auftritt, da mehrere Prozesse versuchen, in dieselbe Datei zu schreiben.
Haupt
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using BaileySoft.Utility;
namespace Diagnostic_Commands
{
class Program
{
static void Main(string[] args)
{
// Log file creation
string strDestopPath = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
strDestopPath += "\\Diagnostic_Today.txt";
StreamWriter w = File.AppendText(strDestopPath);
List<String> commands = new List<String>();
commands.Add("nslookup www.google.com");
commands.Add("Tracert -d www.apple.com");
commands.Add("ipconfig /all");
commands.Add("ping www.google.com -n 10");
commands.Add("nslookup www.apple.com");
foreach (string cmd in commands)
{
Thread tReturn = new Thread(() => { Test_Con.runCommand(cmd); });
tReturn.IsBackground = true;
tReturn.Priority = ThreadPriority.AboveNormal;
tReturn.IsBackground = true;
tReturn.Start();
}
Console.ReadLine();
}
}
}
Klasse
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
public static class Test_Con
{
static string d = null;
public static void runCommand(string command)
{
string starttime;
string endtime;
//* Create your Process
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c" + command;
starttime = "Started at " + DateTime.Now + "\n";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
//* Set your output and error (asynchronous) handlers
process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
process.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler);
//* Start process and handlers
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
endtime = "Completed at " + DateTime.Now + "\n";
d+= "========================================================================";
d+= starttime + endtime ;
Console.WriteLine(d);
}
static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
//* Do your stuff with the output (write to console/log/StringBuilder)
Console.WriteLine(outLine.Data); //This will keep writing all the command output irrespective
}
}
}
Je besser Sie tun können, ist eine Art von Sammlung haben (pro Beispiel ein Wörterbuch von pid und string), dann fügen Sie die Ausgänge es und wenn alle Befehle fertig sind, schreiben die Wörterbuchinhalt in die Datei. – Gusman
http://stackoverflow.com/a/8055430/366904 –
http://stackoverflow.com/questions/8035029/how-to-write-in-a-single-file-with-multiple-threads –