public static int Main(string[] args) { if (args.Length != 2) { Console.Error.WriteLine("Usage: {0} <label> <mount point>", Process.GetCurrentProcess().ProcessName); return(1); } string label = args[0], point = args[1]; List <string> volumes = VolumeUtils.GetVolumeNames(); List <string> matchingVolumes = volumes.Where( volume => VolumeUtils.GetVolumeLabel(volume) == label).ToList(); if (matchingVolumes.Count == 1) { string volume = matchingVolumes[0]; return(VolumeUtils.DeleteAllVolumePaths(volume) && VolumeAPI.SetVolumeMountPoint(point, volume) ? 0 : 3); } else { Console.Error.WriteLine( "Should be exactly one volume with label '{0}', got {1}", label, matchingVolumes.Count); return(2); } }
public static string GetVolumeLabel(string volume) { int unused; StringBuilder label = new StringBuilder(VolumeAPI.MAX_LENGTH); bool rv = VolumeAPI.GetVolumeInformation( volume, label, VolumeAPI.MAX_LENGTH, out unused, out unused, out unused, null, 0); return(rv ? label.ToString() : null); }
private static string GetVolumeLabel(DirectoryInfo dir) { var volume = VolumeAPI.GetVolumeInformation(dir); var isRoot = dir.FullName == dir.Root.FullName; if (isRoot && !string.IsNullOrEmpty(volume.Label)) { return(volume.Label); } return(dir.Name); }
public static List <string> GetVolumeNames() { StringBuilder name = new StringBuilder(VolumeAPI.MAX_LENGTH); List <string> names = new List <string>(); int handle = VolumeAPI.FindFirstVolume(name, VolumeAPI.MAX_LENGTH); do { names.Add(name.ToString()); } while (VolumeAPI.FindNextVolume(handle, name, VolumeAPI.MAX_LENGTH)); return(names); }
public static List <string> GetVolumePaths(string volume) { byte[] raw_paths = new byte[VolumeAPI.MAX_LENGTH]; int actualLength; bool rv = false; unsafe { fixed(byte *paths_ptr = raw_paths) { rv = VolumeAPI.GetVolumePathNamesForVolumeName( volume, paths_ptr, raw_paths.Length, out actualLength); } } return(rv ? ParseRawPaths(raw_paths, actualLength) : null); }
public static bool DeleteAllVolumePaths(string volume) { List <string> paths = GetVolumePaths(volume); if (paths == null) { return(false); } foreach (string path in paths) { if (!VolumeAPI.DeleteVolumeMountPoint(path)) { return(false); } } return(true); }