SHOW:
|
|
- or go back to the newest paste.
| 1 | using System; | |
| 2 | using System.Collections.Generic; | |
| 3 | using System.Linq; | |
| 4 | using System.Text; | |
| 5 | using System.IO; | |
| 6 | using System.Diagnostics; | |
| 7 | using System.Threading; | |
| 8 | ||
| 9 | namespace sticky | |
| 10 | {
| |
| 11 | internal class Program | |
| 12 | {
| |
| 13 | private static void Main(string[] args) | |
| 14 | {
| |
| 15 | doStuff(); | |
| 16 | } | |
| 17 | ||
| 18 | //volume label to look for. could potentially have it read a text file or something | |
| 19 | private static List<string> validVolumeLabels = new List<string>(){ "helix3pro" };
| |
| 20 | private static string uNetBootEXE = "unetbootin-windows-563.exe"; | |
| 21 | private static string formatCommandTemplate = "format $DRIVELETTER /FS:FAT32 /V:helix3pro /Q /y"; | |
| 22 | private static string unetbootParams = | |
| 23 | "method=diskimage isofile=\"$ISOFILE\" installtype=USB targetdrive=$DRIVELETTER nodistro=y exitoncompletion=y autoinstall=y"; | |
| 24 | ||
| 25 | private static void doStuff() | |
| 26 | {
| |
| 27 | //locate unetboot | |
| 28 | Console.WriteLine("Looking for unetbootin executable (" + uNetBootEXE + ") in " + Directory.GetCurrentDirectory() + " ...");
| |
| 29 | if (!File.Exists(uNetBootEXE)) | |
| 30 | {
| |
| 31 | Console.WriteLine(uNetBootEXE + " not found in directory."); | |
| 32 | Console.ReadLine(); | |
| 33 | Environment.Exit(0); | |
| 34 | } | |
| 35 | ||
| 36 | //identify the ISO | |
| 37 | //string isoFile = @"C:\VMWare_Share\helix3custom\Helix3ProR2Custom-v1_2011-10-27.iso"; | |
| 38 | string isoFile = ""; | |
| 39 | ||
| 40 | //Another example of locating your ISO file | |
| 41 | ||
| 42 | //IEnumerable<FileInfo> lwFi = new DirectoryInfo(@"\\foo\goase").GetFiles("*.iso", SearchOption.AllDirectories);
| |
| 43 | //var lwQry = (from file in lwFi | |
| 44 | // orderby file.LastWriteTime descending | |
| 45 | // select file.FullName).SingleOrDefault(); | |
| 46 | ||
| 47 | //isoFile = lwQry; | |
| 48 | ||
| 49 | ||
| 50 | Console.WriteLine("Searching \"" + Directory.GetCurrentDirectory() + "\" for ISO files...");
| |
| 51 | DirectoryInfo di = new DirectoryInfo(Directory.GetCurrentDirectory()); | |
| 52 | IEnumerable<FileInfo> fi = di.GetFiles(); | |
| 53 | var isoQry = from file in fi | |
| 54 | where file.Extension == ".iso" | |
| 55 | select file.FullName; | |
| 56 | ||
| 57 | if (isoQry.Count() > 1) | |
| 58 | {
| |
| 59 | Console.WriteLine("More than one ISO file found. I'm confused. Please fix and re-run program.");
| |
| 60 | Console.ReadLine(); | |
| 61 | Environment.Exit(0); | |
| 62 | } | |
| 63 | if (isoQry.Count() == 0) | |
| 64 | {
| |
| 65 | Console.WriteLine("No ISO files found. I'm confused. Please fix and re-run program.");
| |
| 66 | Console.ReadLine(); | |
| 67 | Environment.Exit(0); | |
| 68 | } | |
| 69 | ||
| 70 | isoFile = isoQry.FirstOrDefault(); | |
| 71 | ||
| 72 | Console.WriteLine("");
| |
| 73 | Console.WriteLine("Using ISO: " + isoFile);
| |
| 74 | Console.WriteLine("");
| |
| 75 | ||
| 76 | //find the drives, confirm with user | |
| 77 | List<string> processTheseDrives = listDrivesToWipe(validVolumeLabels); | |
| 78 | if (processTheseDrives == null || processTheseDrives.Count == 0) | |
| 79 | {
| |
| 80 | Console.WriteLine("No removable drives matching Volume Name Criteria were found!");
| |
| 81 | Console.ReadLine(); | |
| 82 | Environment.Exit(0); | |
| 83 | } | |
| 84 | ||
| 85 | //format drives FAT32 | |
| 86 | Console.WriteLine("Okay, good. Starting wiping...");
| |
| 87 | Console.WriteLine("");
| |
| 88 | ||
| 89 | ||
| 90 | foreach (string driveLetter in processTheseDrives) | |
| 91 | {
| |
| 92 | Console.WriteLine("Formatting drive " + driveLetter);
| |
| 93 | //string formatCommand = @"format " + driveLetter.Replace(@"\", "") + " /FS:FAT32 /V:helix3pro /Q /y"; | |
| 94 | string formatCommand = formatCommandTemplate.Replace("$DRIVELETTER", driveLetter.Replace(@"\", ""));
| |
| 95 | ProcessStartInfo pInfo = new ProcessStartInfo("cmd.exe", "/c " + formatCommand);
| |
| 96 | pInfo.RedirectStandardOutput = true; | |
| 97 | pInfo.UseShellExecute = false; | |
| 98 | pInfo.CreateNoWindow = true; | |
| 99 | ||
| 100 | Process P = new Process(); | |
| 101 | P.StartInfo = pInfo; | |
| 102 | P.OutputDataReceived += exeOutputEventHandler; | |
| 103 | P.Start(); | |
| 104 | ||
| 105 | P.BeginOutputReadLine(); | |
| 106 | P.WaitForExit(); | |
| 107 | P.Close(); | |
| 108 | Console.WriteLine(); | |
| 109 | } | |
| 110 | ||
| 111 | //run unetbootin | |
| 112 | Console.WriteLine("UNetBootin' ISO image to drives...");
| |
| 113 | Console.WriteLine(); | |
| 114 | ||
| 115 | foreach (string driveLetter in processTheseDrives) | |
| 116 | {
| |
| 117 | Console.WriteLine("UNetBootin' drive " + driveLetter);
| |
| 118 | ||
| 119 | Thread uNetBootThread = (new Thread(() => | |
| 120 | {
| |
| 121 | ProcessStartInfo p2info = | |
| 122 | new ProcessStartInfo(uNetBootEXE, | |
| 123 | unetbootParams.Replace( | |
| 124 | "$DRIVELETTER", driveLetter). | |
| 125 | Replace("$ISOFILE", isoFile));
| |
| 126 | ||
| 127 | p2info.RedirectStandardOutput = false; | |
| 128 | p2info.UseShellExecute = true; | |
| 129 | p2info.CreateNoWindow = false; | |
| 130 | ||
| 131 | Process p2 = new Process(); | |
| 132 | p2.StartInfo = p2info; | |
| 133 | p2.Start(); | |
| 134 | p2.WaitForExit(); | |
| 135 | p2.Close(); | |
| 136 | }) {Name = "UNetBootThread"});
| |
| 137 | uNetBootThread.Start(); | |
| 138 | while (uNetBootThread.IsAlive) | |
| 139 | {
| |
| 140 | //it's friday, friday, gotta get down on friday. | |
| 141 | } | |
| 142 | ||
| 143 | } | |
| 144 | ||
| 145 | Console.WriteLine("********** DONE **********");
| |
| 146 | ||
| 147 | //Are we done? | |
| 148 | string exitProgram = ""; | |
| 149 | while (exitProgram != "y" | exitProgram != "n" | exitProgram != "yes" | exitProgram != "no") | |
| 150 | {
| |
| 151 | Console.Write("Done! Start a new batch? (or exit) [y/n]: ");
| |
| 152 | exitProgram = Console.ReadLine().ToLower(); | |
| 153 | if (exitProgram == "yes" || exitProgram == "y") | |
| 154 | {
| |
| 155 | doStuff(); | |
| 156 | } | |
| 157 | else if (exitProgram == "no" || exitProgram == "n") | |
| 158 | {
| |
| 159 | return; | |
| 160 | } | |
| 161 | } | |
| 162 | ||
| 163 | } | |
| 164 | ||
| 165 | /// <summary> | |
| 166 | /// Checks logical drives for possible wipe/ISO targets | |
| 167 | /// </summary> | |
| 168 | /// <returns></returns> | |
| 169 | private static List<string> listDrivesToWipe(List<string> validVolumeLabels) | |
| 170 | {
| |
| 171 | StringBuilder strValidLabels = new StringBuilder(); | |
| 172 | foreach(string validLabel in validVolumeLabels) | |
| 173 | {
| |
| 174 | strValidLabels.Append("\"" + validLabel + "\"" + " ");
| |
| 175 | } | |
| 176 | ||
| 177 | Console.WriteLine("Searching for removable drives with volume labels containing: " + strValidLabels);
| |
| 178 | List<DriveInfo> drivesAll = DriveInfo.GetDrives().ToList(); | |
| 179 | List<string> drivesToProcess = new List<string>(); | |
| 180 | ||
| 181 | Console.WriteLine("Listing possible target drives...");
| |
| 182 | Console.WriteLine("");
| |
| 183 | Console.WriteLine("Drive\tVolume Label\tFS\tType\t\tSize Info");
| |
| 184 | Console.WriteLine("-----\t------------\t--\t----\t\t---------");
| |
| 185 | ||
| 186 | foreach (var drv in drivesAll) | |
| 187 | {
| |
| 188 | if (drv.IsReady) | |
| 189 | {
| |
| 190 | if (validVolumeLabels.Contains(drv.VolumeLabel, StringComparer.OrdinalIgnoreCase)) | |
| 191 | {
| |
| 192 | StringBuilder driveInformation = new StringBuilder(); | |
| 193 | driveInformation.Append(drv.Name); | |
| 194 | driveInformation.Append("\t" + drv.VolumeLabel);
| |
| 195 | driveInformation.Append("\t" + drv.DriveFormat);
| |
| 196 | driveInformation.Append("\t" + drv.DriveType.ToString());
| |
| 197 | driveInformation.Append("\t" + bytesToGB(drv.AvailableFreeSpace).ToString("00.00"));
| |
| 198 | driveInformation.Append(" / ");
| |
| 199 | driveInformation.Append(bytesToGB(drv.TotalSize).ToString("00.00"));
| |
| 200 | driveInformation.Append(" GB");
| |
| 201 | ||
| 202 | Console.WriteLine(driveInformation.ToString()); | |
| 203 | drivesToProcess.Add(drv.Name); | |
| 204 | } | |
| 205 | } | |
| 206 | } | |
| 207 | if (drivesToProcess.Count() == 0) | |
| 208 | {
| |
| 209 | Console.WriteLine("No valid removable drives were found matching the volume label criteria.");
| |
| 210 | Console.ReadLine(); | |
| 211 | Environment.Exit(0); | |
| 212 | } | |
| 213 | ||
| 214 | Console.WriteLine("");
| |
| 215 | Console.Write("Are these the correct drives? [y/n]");
| |
| 216 | string usrInput = Console.ReadLine(); | |
| 217 | if (usrInput.Trim().ToLower() != "y") | |
| 218 | {
| |
| 219 | return null; | |
| 220 | } | |
| 221 | ||
| 222 | return drivesToProcess; | |
| 223 | } | |
| 224 | ||
| 225 | /// <summary> | |
| 226 | /// Convert bytes to GB | |
| 227 | /// </summary> | |
| 228 | /// <param name="bytes"></param> | |
| 229 | /// <returns></returns> | |
| 230 | private static double bytesToGB(long bytes) | |
| 231 | {
| |
| 232 | double ret = ((bytes/1024f)/1024f)/1024f; | |
| 233 | return ret; | |
| 234 | } | |
| 235 | ||
| 236 | private static int numOutputLines = 0; | |
| 237 | ||
| 238 | /// <summary> | |
| 239 | /// Event handler to redirect out CLI output | |
| 240 | /// </summary> | |
| 241 | /// <param name="sendingProcess"></param> | |
| 242 | /// <param name="outLine"></param> | |
| 243 | private static void exeOutputEventHandler(object sendingProcess, | |
| 244 | DataReceivedEventArgs outLine) | |
| 245 | {
| |
| 246 | if (!String.IsNullOrEmpty(outLine.Data)) | |
| 247 | {
| |
| 248 | numOutputLines++; | |
| 249 | ||
| 250 | Console.WriteLine("[" + numOutputLines.ToString("000") + "] - " + outLine.Data);
| |
| 251 | ||
| 252 | } | |
| 253 | } | |
| 254 | } | |
| 255 | } | |
| 256 | ||
| 257 | ||
| 258 |