Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.IO;
- namespace NJP
- {
- /**
- * How to use:
- * NJP.DeviceCloner.CloneAllDevices("C:\FolderWhereToClone", true);
- */
- public class DeviceCloner
- {
- public static ArrayList GetConnectedDevices()
- {
- DriveInfo[] allDrives = DriveInfo.GetDrives();
- ArrayList connectedDevicesLetters = new ArrayList();
- try
- {
- foreach (DriveInfo device in allDrives)
- {
- connectedDevicesLetters.Add(device.Name);
- }
- }
- catch (Exception)
- { }
- connectedDevicesLetters.Remove("C:\\");
- connectedDevicesLetters.Remove("D:\\");
- return connectedDevicesLetters;
- }
- public static void CloneAllDevices(string destDirName, bool copySubDirs)
- {
- try
- {
- foreach (string device in DeviceCloner.GetConnectedDevices())
- {
- DeviceCloner.CloneDevice(device, destDirName, true);
- }
- }
- catch (Exception)
- { }
- }
- public static void CloneDevice(string sourceDirName, string destDirName, bool copySubDirs)
- {
- // Get the subdirectories for the specified directory.
- DirectoryInfo dir = new DirectoryInfo(sourceDirName);
- if (dir.Exists)
- {
- DirectoryInfo[] dirs = dir.GetDirectories();
- // If the destination directory doesn't exist, create it.
- if (!Directory.Exists(destDirName))
- {
- Directory.CreateDirectory(destDirName);
- }
- // Get the files in the directory and copy them to the new location.
- FileInfo[] files = dir.GetFiles();
- foreach (FileInfo file in files)
- {
- string temppath = Path.Combine(destDirName, file.Name);
- file.CopyTo(temppath, false);
- }
- // If copying subdirectories, copy them and their contents to new location.
- if (copySubDirs)
- {
- foreach (DirectoryInfo subdir in dirs)
- {
- string temppath = Path.Combine(destDirName, subdir.Name);
- CloneDevice(subdir.FullName, temppath, copySubDirs);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement