Für den Anfang mal als Konsolenanwendung:
Alles anzeigen
Was gesichert werden soll, trägst Du einfach in die Datei "paths.txt" ein (hier im Beispiel liegt die auf "D:\") - nach dem Format:
QuellDatei (oder Verzeichnis)|Zielverzeichnis
und je Zeile eine Angabe; also beispielsweise so:
D:\quelldatei1.txt|F:\sicherung1\
D:\quelldatei2.txt|F:\sicherung1\
D:\quellverzeichnis|F:\sicherung2\
D:\quelldatei2.txt|F:\sicherung2\
Unterverzeichnisse werden aktuell nicht beachtet und ausprobiert habe ich das ganze auch noch nicht
sollte aber funktionieren.
C#-Quellcode
- using System.IO;
- namespace Backup
- {
- class Program
- {
- static void Main(string[] args)
- {
- string file = @"D:\paths.txt";
- FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
- StreamReader sr = new StreamReader(fs);
- string[] paths = new string[2];
- while(!sr.EndOfStream)
- {
- string path = sr.ReadLine().ToString();
- if (path != string.Empty)
- {
- paths = path.Split('|');
- Copy(paths);
- }
- }
- }
- private static void Copy(string[] paths)
- {
- FileAttributes fa = File.GetAttributes(paths[0]);
- if (fa.HasFlag(FileAttributes.Directory))
- {
- CopyDirectory(paths);
- }
- else if (!fa.HasFlag(FileAttributes.Directory))
- {
- File.Copy(paths[0], paths[1], true);
- }
- }
- private static void CopyDirectory(string[] paths)
- {
- DirectoryInfo diSource = new DirectoryInfo(paths[0]);
- DirectoryInfo diDestination = new DirectoryInfo(paths[1]);
- if (!diSource.Exists)
- {
- // Überleg Dir was passieren soll, wenns das Quell- Verzeichnis nicht gibt.
- }
- if (!diDestination.Exists)
- {
- Directory.CreateDirectory(paths[1]);
- }
- FileInfo[] files = diSource.GetFiles();
- foreach (FileInfo fi in files)
- {
- string temppath = Path.Combine(paths[1], fi.Name);
- fi.CopyTo(temppath, true);
- }
- }
- }
- }
QuellDatei (oder Verzeichnis)|Zielverzeichnis
und je Zeile eine Angabe; also beispielsweise so:
D:\quelldatei1.txt|F:\sicherung1\
D:\quelldatei2.txt|F:\sicherung1\
D:\quellverzeichnis|F:\sicherung2\
D:\quelldatei2.txt|F:\sicherung2\
Unterverzeichnisse werden aktuell nicht beachtet und ausprobiert habe ich das ganze auch noch nicht
