c# Robocopy Mirror

  • C#

Es gibt 12 Antworten in diesem Thema. Der letzte Beitrag () ist von Andinistrator.

    c# Robocopy Mirror

    Hallo Zusammen,

    ich möchte per Knopfdruck ein Verzeichnis spiegeln. Weil ich das Rad noch nicht neu erfinden kann (zu wenig Erfahrung), möchte ich robocopy verwenden.

    Dazu folgender Code:

    Spoiler anzeigen

    C#-Quellcode

    1. private void Spiegel1()
    2. {
    3. // Spiegel1 führt CMD aus
    4. ProcessStartInfo Spiegel1 = new ProcessStartInfo("cmd.exe");
    5. // Fenster unsichtbar machen
    6. Spiegel1.WindowStyle = ProcessWindowStyle.Hidden;
    7. Spiegel1.CreateNoWindow = true;
    8. // Pfad eintragen Quelle Ziel /MIR = spiegeln per robocopy
    9. Spiegel1.Arguments = "/c Robocopy.exe " + @"Ordner1 C:\Ordner2 /MIR";
    10. Process.Start(Spiegel1);
    11. }
    12. private void btnSpiegel_Click(object sender, EventArgs e)
    13. {
    14. // Button startet:
    15. Spiegel1();
    16. }


    Leider will das so nicht klappen, d.h. es wird nix von der Ordner1 nach Ordner2 gebracht. Hat hier jemand einen Tipp?
    Robocopy ist ein mächtiges Admin-Tool, also lies erstmal die Dokumentation.
    Dazu starte RoboCopy über die Kommandozeilen-Eingabe, und gib ? ein.
    Das dann auskopieren in einen Editor, und in aller Ruhe studieren.
    Oder google dir die Doku.

    Zum ausführen per vb.net ist die cmd.exe nicht erforderlich, sondern da rufst du RoboCopy direkt auf.

    BeispielCode findest du in [OpenSource] Sync - Tool für Dateisystem-Backups und -Synchronisierungen
    Ich glaub ich hab sogar die Doku-Datei auch noch drinne.
    Robocopy per CMD ist mir klar.

    robocopy C:\Ordner1 C:\Ordner2 /MIR /W:5 /R:2


    Google hat mir schon etwas geholfen, nur leider nicht ganz:
    social.msdn.microsoft.com/Foru…ith-c?forum=csharpgeneral

    Spoiler anzeigen

    C#-Quellcode

    1. private void Spiegel2()
    2. {
    3. Process Robocopy = new Process();
    4. Robocopy.StartInfo.FileName = "robocopy";
    5. Robocopy.StartInfo.Arguments = @"C:\Ordner1" + " " + @"C:\Ordner2" + " /MIR /W:5 /R:2";
    6. Robocopy.Start();
    7. Robocopy.WaitForExit();
    8. Robocopy.Close();
    9. }


    Später möchte ich die Pfade aus der Textbox ziehen, momentan hänge ich auch damit fest.
    Wenn man den robocopy in einen CMD eingibt stimme ich dir zu.

    Ich hab nochmal ein neues Projekt geöffnet, um es zu püfen.
    Element: 1 Button

    Spoiler anzeigen

    C#-Quellcode

    1. using System.Collections.Generic;
    2. using System.ComponentModel;
    3. using System.Data;
    4. using System.Diagnostics;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Threading.Tasks;
    9. using System.Windows.Forms;
    10. namespace robocopy
    11. {
    12. public partial class Form1 : Form
    13. {
    14. public Form1()
    15. {
    16. InitializeComponent();
    17. }
    18. private void button1_Click(object sender, EventArgs e)
    19. {
    20. Spiegeln();
    21. }
    22. private void Spiegeln()
    23. {
    24. Process Robocopy = new Process();
    25. Robocopy.StartInfo.FileName = "robocopy";
    26. Robocopy.StartInfo.Arguments = @"C:\Ordner1" + " " + @"C:\Ordner2" + " /MIR /W:5 /R:2";
    27. Robocopy.Start();
    28. Robocopy.WaitForExit();
    29. Robocopy.Close();
    30. }
    31. }
    32. }


    Jetzt öffnen sich beim Button Click eine zweite Form... .

    Bluespide schrieb:

    Dein Code funktioniert bei mir super. Wo ist jetzt das Problem?


    Post mal bitte welcher, also bei mir passiert da nix. Ich habe nochmal eine Form mit nur einem Button erstellt... da tut sich nix, d.h. es wird nichts nach Ordner2 gespiegelt.

    Ich möchte quasi eine Kombination aus Textboxen, in welchem die Pfade sind per C# ausführen lassen:

    // Textbox1 = C:\Ordner1
    // Textbox2 = C:\Ordner2
    // Textbox3 = *.txt

    "robocopy C:\Ordner1 C:\Ordner2 *.txt /MIR"
    oder
    robocopy textbox1.text + textbox2.text + textbox3.text + "/MIR";

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „Andinistrator“ ()

    Ich freu mich natürlich das es bei Bluespide funktioniert. Ich habe aber folgendes Problem:

    1 Form
    1 Button

    Folgender Befehl öffnen nur eine neue Form:
    Spoiler anzeigen

    C#-Quellcode

    1. private void Spiegel()
    2. {
    3. Process prozess1 = new Process();
    4. prozess1.StartInfo.FileName = "robocopy";
    5. prozess1.StartInfo.Arguments = @" C:\Ordner1 C:\Ordner2 /MIR";
    6. prozess1.Start();
    7. }


    Folgender Befehlt tut einfach mal nix:
    Spoiler anzeigen

    C#-Quellcode

    1. private void Spiegel1()
    2. {
    3. // Spiegel1 führt CMD aus
    4. ProcessStartInfo Spiegel1 = new ProcessStartInfo("cmd.exe");
    5. // Fenster unsichtbar machen
    6. Spiegel1.WindowStyle = ProcessWindowStyle.Hidden;
    7. // Spiegel1.CreateNoWindow = true;
    8. // Pfad eintragen Quelle Ziel /MIR = spiegeln per robocopy
    9. Spiegel1.Arguments = "robocopy" + @"C:\Ordner1 " + @"C:\Ordner2" + "/MIR";
    10. // Process.Start(Spiegel1);
    11. }


    Folgender Befehl öffnen auch eine Form:
    Spoiler anzeigen

    C#-Quellcode

    1. private void Spiegel2()
    2. {
    3. Process Robocopy = new Process();
    4. Robocopy.StartInfo.FileName = "robocopy";
    5. Robocopy.StartInfo.Arguments = @"C:\Ordner1" + " " + @"C:\Ordner2" + " /MIR /W:5 /R:2";
    6. Robocopy.StartInfo.CreateNoWindow = true;
    7. Robocopy.StartInfo.UseShellExecute = false;
    8. Robocopy.Start();
    9. Robocopy.WaitForExit();
    10. Robocopy.Close();
    11. }


    Es wird nichts von Ordner1 nach Ordner2 gespiegelt. Kann mir jemand eine Code nenne, mit dem es gespiegelt wird?