public static async Task <StorageFolderWithPath> GetRootFromPath(string devicePath) { if (!Path.IsPathRooted(devicePath)) { return(null); } var rootPath = Path.GetPathRoot(devicePath); if (devicePath.StartsWith("\\\\?\\")) { // Check among already discovered drives StorageFolder matchingDrive = App.AppSettings.DrivesManager.Drives.FirstOrDefault(x => InstanceTabsView.NormalizePath(x.Path) == InstanceTabsView.NormalizePath(rootPath))?.Root; if (matchingDrive == null) { // Check on all removable drives var remDevices = await DeviceInformation.FindAllAsync(StorageDevice.GetDeviceSelector()); foreach (var item in remDevices) { try { var root = StorageDevice.FromId(item.Id); if (InstanceTabsView.NormalizePath(rootPath).Replace("\\\\?\\", "") == root.Name.ToUpperInvariant()) { matchingDrive = root; break; } } catch (Exception) { // Ignore this.. } } } if (matchingDrive != null) { return(new StorageFolderWithPath(matchingDrive, rootPath)); } } // It's ok to return null here, on normal drives StorageFolder.GetFolderFromPathAsync works //else //{ // return new StorageFolderWithPath(await StorageFolder.GetFolderFromPathAsync(rootPath), rootPath); //} return(null); }
private async Task GetDrives(IList <DriveItem> list) { var drives = DriveInfo.GetDrives().ToList(); var remDevices = await DeviceInformation.FindAllAsync(StorageDevice.GetDeviceSelector()); var supportedDevicesNames = remDevices.Select(x => StorageDevice.FromId(x.Id).Name); foreach (DriveInfo driveInfo in drives.ToList()) { if (!supportedDevicesNames.Contains(driveInfo.Name) && driveInfo.DriveType == System.IO.DriveType.Removable) { drives.Remove(driveInfo); } } foreach (var drive in drives) { // If drive already in list, skip. if (list.Any(x => x.tag == drive.Name)) { continue; } var folder = Task.Run(async() => await StorageFolder.GetFolderFromPathAsync(drive.Name)).Result; DriveType type = DriveType.Unkown; switch (drive.DriveType) { case System.IO.DriveType.CDRom: type = DriveType.CDRom; break; case System.IO.DriveType.Fixed: if (InstanceTabsView.NormalizePath(drive.Name) != InstanceTabsView.NormalizePath("A:") && InstanceTabsView.NormalizePath(drive.Name) != InstanceTabsView.NormalizePath("B:")) { type = DriveType.Fixed; } else { type = DriveType.FloppyDisk; } break; case System.IO.DriveType.Network: type = DriveType.Network; break; case System.IO.DriveType.NoRootDirectory: type = DriveType.NoRootDirectory; break; case System.IO.DriveType.Ram: type = DriveType.Ram; break; case System.IO.DriveType.Removable: type = DriveType.Removable; break; case System.IO.DriveType.Unknown: type = DriveType.Unkown; break; default: type = DriveType.Unkown; break; } var driveItem = new DriveItem( folder, Visibility.Visible, type); Logger.Info($"Drive added: {driveItem.tag}, {driveItem.Type}"); list.Add(driveItem); } }
private void GetDrives(IList <DriveItem> list) { var drives = DriveInfo.GetDrives(); foreach (var drive in drives) { // If drive already in list, skip. if (list.Any(x => x.tag == drive.Name)) { continue; } var folder = Task.Run(async() => await StorageFolder.GetFolderFromPathAsync(drive.Name)).Result; DriveType type = DriveType.Unkown; switch (drive.DriveType) { case System.IO.DriveType.CDRom: type = DriveType.CDRom; break; case System.IO.DriveType.Fixed: if (InstanceTabsView.NormalizePath(drive.Name) != InstanceTabsView.NormalizePath("A:") && InstanceTabsView.NormalizePath(drive.Name) != InstanceTabsView.NormalizePath("B:")) { type = DriveType.Fixed; } else { type = DriveType.FloppyDisk; } break; case System.IO.DriveType.Network: type = DriveType.Network; break; case System.IO.DriveType.NoRootDirectory: type = DriveType.NoRootDirectory; break; case System.IO.DriveType.Ram: type = DriveType.Ram; break; case System.IO.DriveType.Removable: type = DriveType.Removable; break; case System.IO.DriveType.Unknown: type = DriveType.Unkown; break; default: type = DriveType.Unkown; break; } var driveItem = new DriveItem( folder, Visibility.Visible, type); list.Add(driveItem); } }
private async Task GetDrives(IList <DriveItem> list) { var drives = DriveInfo.GetDrives().ToList(); var remDevices = await DeviceInformation.FindAllAsync(StorageDevice.GetDeviceSelector()); List <string> supportedDevicesNames = new List <string>(); foreach (var item in remDevices) { try { supportedDevicesNames.Add(StorageDevice.FromId(item.Id).Name); } catch (Exception e) { Logger.Warn("Can't get storage device name: " + e.Message + ", skipping..."); } } foreach (DriveInfo driveInfo in drives.ToList()) { if (!supportedDevicesNames.Contains(driveInfo.Name) && driveInfo.DriveType == System.IO.DriveType.Removable) { drives.Remove(driveInfo); } } foreach (var drive in drives) { // If drive already in list, skip. if (list.Any(x => x.Path == drive.Name)) { continue; } var folder = Task.Run(async() => await StorageFolder.GetFolderFromPathAsync(drive.Name)).Result; DriveType type = DriveType.Unknown; switch (drive.DriveType) { case System.IO.DriveType.CDRom: type = DriveType.CDRom; break; case System.IO.DriveType.Fixed: if (InstanceTabsView.NormalizePath(drive.Name) != InstanceTabsView.NormalizePath("A:") && InstanceTabsView.NormalizePath(drive.Name) != InstanceTabsView.NormalizePath("B:")) { type = DriveType.Fixed; } else { type = DriveType.FloppyDisk; } break; case System.IO.DriveType.Network: type = DriveType.Network; break; case System.IO.DriveType.NoRootDirectory: type = DriveType.NoRootDirectory; break; case System.IO.DriveType.Ram: type = DriveType.Ram; break; case System.IO.DriveType.Removable: type = DriveType.Removable; break; case System.IO.DriveType.Unknown: type = DriveType.Unknown; break; default: type = DriveType.Unknown; break; } var driveItem = new DriveItem(folder, type); Logger.Info($"Drive added: {driveItem.Path}, {driveItem.Type}"); list.Add(driveItem); } }