In Shell Befehle ausführen

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • In Shell Befehle ausführen

    Hallo,

    wie kann ich in einem .Net Programm ein Windows Shell-Fenster öffnen und darin mehrer Befehle ausführen.

    Es gibt den Befehl "shell" und eine "System.Diagnostics" Klasse mit denen das gehen sollte, aber beides funktioniert bei mir nur wenn ich einen Befehl ausführe. Sobald ich mehr als einen befehl ausführen will klappt's nicht mehr.

    70abc
    We raise hopes, here ... until they're old enough to fend for themselves.
    - Mike Callahan
  • Hi,
    was heißt für dich mehrere Befehle?
    So wie ich das sehe, kannst du einen Prozess starten und in diesem Prozess mehrmals eine Eingabe mit WriteLine() senden.
    Zwischendrin musst du (ne nach Anwendung) den Puffer leeren indem du ReadLine() ausführst.

    Quellcode

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Diagnostics;
    4. public class MyClass
    5. {
    6. public static void Main()
    7. {
    8. try {
    9. ProcessStartInfo psi = new ProcessStartInfo("C:\Windows\system32\cmd.exe");
    10. psi.UseShellExecute = false;
    11. psi.CreateNoWindow = true;
    12. psi.RedirectStandardInput = true;
    13. psi.RedirectStandardOutput = true;
    14. //psi.RedirectStandardError = true;
    15. Process process = new Process();
    16. process.StartInfo = psi;
    17. bool started = process.Start();
    18. if (started)
    19. {
    20. process.StandardOutput.ReadLine(); // "Microsoft Windows"
    21. process.StandardOutput.ReadLine(); // "Copyright Microsoft"
    22. process.StandardOutput.ReadLine(); // [blank line following logo]
    23. process.StandardOutput.ReadLine(); // [command entry (echo)]
    24. process.StandardInput.WriteLine("echo Blah");
    25. process.StandardInput.Flush();
    26. string ret = process.StandardOutput.ReadLine(); // <-- stalls here
    27. System.Console.WriteLine("CMD.exe says " + ret + "\".");
    28. }
    29. } catch (Exception e) {
    30. }
    31. }
    32. }
    Alles anzeigen

    msnewsgroups.net/group/microso…es.csharp/topic37227.aspx