Entwickler-Ecke

IO, XML und Registry - Shell Kommandos mit C# ausführen


Scofield2011 - Sa 29.06.13 19:40
Titel: Shell Kommandos mit C# ausführen
Hallo Leute,

ich habe eine Frage. Und zwar habe ich in einem VBA Projekt den folgenden Code:


C#-Quelltext
1:
2:
3:
Shell "net use \\" & IP & "\test /del"1
Shell "net use \\" & IP & "\test /USER:test123 pass123"1
Shell "explorer \\" & IP & "\test\", 1


Gibt es in C# eine Methode, mit der sich ähnlich komfortabel Shell Kommandos ausführen lassen?

Vielen Dank schon einmal im Voraus für eure Hilfe.

Scofield2011


jfheins - Sa 29.06.13 21:20

Naja, genau so komfortabel geht es nicht.
Genauer gesagt musst du die cmd.exe manuell aufrufen, wie in dieser SO Frahe gezeigt: http://stackoverflow.com/questions/1469764/run-command-prompt-commands/1469790#1469790
Um das Konsolenfesnder zu verbergen also:

C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = @"/C explorer \\" + IP + @"\test\";
process.StartInfo = startInfo;
process.Start();

Das kannst du natürlich relativ einfach in eine Methode kapseln.