private async static void NetworkDriveCheckTimer_Tick(object sender, object e) { NetworkDriveCheckTimer.Stop(); DriveInfo[] NewNetworkDrive = DriveInfo.GetDrives().Where((Drives) => Drives.DriveType == DriveType.Network).ToArray(); DriveRelatedData[] ExistNetworkDrive = DriveList.Where((ExistDrive) => ExistDrive.DriveType == DriveType.Network).ToArray(); IEnumerable <DriveInfo> AddList = NewNetworkDrive.Where((NewDrive) => ExistNetworkDrive.All((ExistDrive) => ExistDrive.Folder.Path != NewDrive.RootDirectory.FullName)); IEnumerable <DriveRelatedData> RemoveList = ExistNetworkDrive.Where((ExistDrive) => NewNetworkDrive.All((NewDrive) => ExistDrive.Folder.Path != NewDrive.RootDirectory.FullName)); foreach (DriveRelatedData ExistDrive in RemoveList) { DriveList.Remove(ExistDrive); } foreach (DriveInfo Drive in AddList) { try { StorageFolder Device = await StorageFolder.GetFolderFromPathAsync(Drive.RootDirectory.FullName); DriveList.Add(await DriveRelatedData.CreateAsync(Device, Drive.DriveType)); } catch (Exception ex) { LogTracer.Log(ex, $"Hide the device \"{Drive.RootDirectory.FullName}\" for error"); } } NetworkDriveCheckTimer.Start(); }
private async static void NetworkDriveCheckTimer_Tick(object sender, object e) { NetworkDriveCheckTimer.Stop(); DriveInfo[] NewNetworkDrive = DriveInfo.GetDrives().Where((Drives) => Drives.DriveType == DriveType.Network).ToArray(); DriveDataBase[] ExistNetworkDrive = DriveList.OfType <NormalDriveData>().Where((ExistDrive) => ExistDrive.DriveType == DriveType.Network).ToArray(); IEnumerable <DriveInfo> AddList = NewNetworkDrive.Where((NewDrive) => ExistNetworkDrive.All((ExistDrive) => !ExistDrive.Path.Equals(NewDrive.RootDirectory.FullName, StringComparison.OrdinalIgnoreCase))); IEnumerable <DriveDataBase> RemoveList = ExistNetworkDrive.Where((ExistDrive) => NewNetworkDrive.All((NewDrive) => !ExistDrive.Path.Equals(NewDrive.RootDirectory.FullName, StringComparison.OrdinalIgnoreCase))); foreach (DriveDataBase ExistDrive in RemoveList) { DriveList.Remove(ExistDrive); } foreach (DriveInfo Drive in AddList) { try { StorageFolder Device = await StorageFolder.GetFolderFromPathAsync(Drive.RootDirectory.FullName); DriveList.Add(await DriveDataBase.CreateAsync(Device, Drive.DriveType)); } catch (Exception ex) { LogTracer.Log(ex, $"Hide the device \"{Drive.RootDirectory.FullName}\" for error"); } } NetworkDriveCheckTimer.Start(); }
public static async Task LoadDeviceAsync() { foreach (DriveInfo Drive in DriveInfo.GetDrives().Where((Drives) => Drives.DriveType == DriveType.Fixed || Drives.DriveType == DriveType.Removable || Drives.DriveType == DriveType.Network) .Where((NewItem) => DriveList.All((Item) => Item.Folder.Path != NewItem.RootDirectory.FullName))) { try { StorageFolder DriveFolder = await StorageFolder.GetFolderFromPathAsync(Drive.RootDirectory.FullName); DriveList.Add(await DriveRelatedData.CreateAsync(DriveFolder, Drive.DriveType).ConfigureAwait(true)); } catch (Exception ex) { LogTracer.Log(ex, $"Hide the device \"{Drive.RootDirectory.FullName}\" for error"); } } switch (PortalDeviceWatcher.Status) { case DeviceWatcherStatus.Created: case DeviceWatcherStatus.Aborted: case DeviceWatcherStatus.Stopped: { PortalDeviceWatcher.Start(); break; } } NetworkDriveCheckTimer.Start(); }
private void FillDriveList(bool isRefreshing = false) { var list = DriveInfo.GetDrives().Where(x => x.IsReady && (showAllDrives || (x.DriveType == DriveType.Removable && x.DriveFormat.StartsWith("FAT")))).ToArray(); if (isRefreshing) { if (DriveList.Select(x => x.Name).SequenceEqual(list.Select(x => x.Name))) { return; } DriveList.Clear(); } //fill drive list and try to find drive with gdemu contents foreach (DriveInfo drive in list) { DriveList.Add(drive); //look for GDEMU.ini file if (SelectedDrive == null && File.Exists(Path.Combine(drive.RootDirectory.FullName, Constants.MenuConfigTextFile))) { SelectedDrive = drive; } } //look for 01 folder if (SelectedDrive == null) { foreach (DriveInfo drive in list) { if (Directory.Exists(Path.Combine(drive.RootDirectory.FullName, "01"))) { SelectedDrive = drive; break; } } } if (!DriveList.Any()) { return; } if (SelectedDrive == null) { SelectedDrive = DriveList.LastOrDefault(); } }
public static async Task LoadDeviceAsync() { if (Interlocked.Exchange(ref LoadDriveLockResource, 1) == 0) { try { if (!IsDriveLoaded) { IsDriveLoaded = true; foreach (DriveInfo Drive in DriveInfo.GetDrives().Where((Drives) => Drives.DriveType == DriveType.Fixed || Drives.DriveType == DriveType.Removable || Drives.DriveType == DriveType.Network) .Where((NewItem) => DriveList.All((Item) => Item.Folder.Path != NewItem.RootDirectory.FullName))) { try { StorageFolder DriveFolder = await StorageFolder.GetFolderFromPathAsync(Drive.RootDirectory.FullName); DriveList.Add(await DriveRelatedData.CreateAsync(DriveFolder, Drive.DriveType)); } catch (Exception ex) { LogTracer.Log(ex, $"Hide the device \"{Drive.RootDirectory.FullName}\" for error"); } } switch (PortalDeviceWatcher.Status) { case DeviceWatcherStatus.Created: case DeviceWatcherStatus.Aborted: case DeviceWatcherStatus.Stopped: { PortalDeviceWatcher.Start(); break; } } NetworkDriveCheckTimer.Start(); } } finally { _ = Interlocked.Exchange(ref LoadDriveLockResource, 0); } } }
private static async void PortalDeviceWatcher_Added(DeviceWatcher sender, DeviceInformation args) { try { StorageFolder DeviceFolder = StorageDevice.FromId(args.Id); if (DriveList.All((Device) => (string.IsNullOrEmpty(Device.Path) || string.IsNullOrEmpty(DeviceFolder.Path)) ? !Device.Name.Equals(DeviceFolder.Name, StringComparison.OrdinalIgnoreCase) : !Device.Path.Equals(DeviceFolder.Path, StringComparison.OrdinalIgnoreCase))) { await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { DriveList.Add(await DriveDataBase.CreateAsync(DeviceFolder, DriveType.Removable)); }); } } catch (Exception ex) { LogTracer.Log(ex, $"Error happened when add device to HardDeviceList"); } }
/// <summary> /// 增加设备,用于在设备插入后将其添加到列表和处理队列中 /// </summary> private void AddUSBDevices() { var drives = DriveInfo.GetDrives(); string deviceNum = ""; foreach (var drive in drives) { if (drive.IsReady && drive.DriveType == DriveType.Removable && !DriveList.Exists(x => x.Name == drive.Name) && Directory.Exists(drive.Name + MainWindow.devicePath)) { try { deviceNum = drive.Name; if (File.Exists(drive.Name + "Config.ini")) { string[] deviceInfo = File.ReadAllLines(drive.Name + "Config.ini", Encoding.Default); if (deviceInfo.Length > 1 && deviceInfo[0] == "调车") { deviceNum = deviceInfo[1] + "号"; } } string[] files = Directory.GetFiles(drive.Name + MainWindow.devicePath, "YC?????_*.MP4", SearchOption.TopDirectoryOnly); if (files.Length != 0) { deviceNum = System.IO.Path.GetFileName(files[files.GetUpperBound(0)]).Substring(6, 1) + "号"; } } catch { MessageBox.Show("部分U盘设备未连接好,请重新连接"); } AddControl(deviceNum, out ComState comState, out Button button); USBDevice usbDevice = new USBDevice(drive.Name, comState, button, drive.DriveType); DriveList.Add(usbDevice); CopyQueue.Enqueue(usbDevice); } } if (DriveList.Count == 0) { NoDriveState.Visibility = Visibility.Visible; } }
private static async void PortalDeviceWatcher_Added(DeviceWatcher sender, DeviceInformation args) { try { StorageFolder DeviceFolder = StorageDevice.FromId(args.Id); if (DriveList.All((Device) => (string.IsNullOrEmpty(Device.Folder.Path) || string.IsNullOrEmpty(DeviceFolder.Path)) ? Device.Folder.Name != DeviceFolder.Name : Device.Folder.Path != DeviceFolder.Path)) { await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { DriveList.Add(await DriveRelatedData.CreateAsync(DeviceFolder, DriveType.Removable).ConfigureAwait(true)); }); } } catch (Exception ex) { LogTracer.Log(ex, $"Error happened when add device to HardDeviceList"); } }
public void UpdateDrives() { using (var sdd = new StorageDeviceDescriptor()) { var drives = new DriveList(); foreach (var drive in Directory.GetLogicalDrives()) { var deviceName = GetDeviceName(drive); if (sdd.Load(deviceName)) { if (sdd.BusType == STORAGE_BUS_TYPE.BusTypeUsb && sdd.RemovableMedia == true) { drives.Add(drive); } } } Drives = drives; } }
public static async Task LoadDriveAsync(bool IsRefresh = false) { if (Interlocked.Exchange(ref LoadDriveLockResource, 1) == 0) { try { if (!IsDriveLoaded || IsRefresh) { IsDriveLoaded = true; foreach (DriveInfo Drive in DriveInfo.GetDrives().Where((Drives) => Drives.DriveType == DriveType.Fixed || Drives.DriveType == DriveType.Removable || Drives.DriveType == DriveType.Network) .Where((NewItem) => DriveList.All((Item) => !Item.Path.Equals(NewItem.RootDirectory.FullName, StringComparison.OrdinalIgnoreCase)))) { try { StorageFolder Folder = await StorageFolder.GetFolderFromPathAsync(Drive.RootDirectory.FullName); if (DriveList.All((Item) => (string.IsNullOrEmpty(Item.Path) || string.IsNullOrEmpty(Folder.Path)) ? !Item.Name.Equals(Folder.Name, StringComparison.OrdinalIgnoreCase) : !Item.Path.Equals(Folder.Path, StringComparison.OrdinalIgnoreCase))) { DriveList.Add(await DriveDataBase.CreateAsync(Folder, Drive.DriveType)); } } catch (Exception ex) { LogTracer.Log(ex, $"Hide the device \"{Drive.RootDirectory.FullName}\" for error"); } } foreach (DeviceInformation Drive in await DeviceInformation.FindAllAsync(StorageDevice.GetDeviceSelector())) { try { StorageFolder Folder = StorageDevice.FromId(Drive.Id); if (DriveList.All((Item) => (string.IsNullOrEmpty(Item.Path) || string.IsNullOrEmpty(Folder.Path)) ? !Item.Name.Equals(Folder.Name, StringComparison.OrdinalIgnoreCase) : !Item.Path.Equals(Folder.Path, StringComparison.OrdinalIgnoreCase))) { DriveList.Add(await DriveDataBase.CreateAsync(Folder, DriveType.Removable)); } } catch (Exception ex) { LogTracer.Log(ex, $"Hide the device for error"); } } foreach (StorageFolder Folder in await GetWslDriveAsync()) { try { if (DriveList.All((Item) => (string.IsNullOrEmpty(Item.Path) || string.IsNullOrEmpty(Folder.Path)) ? !Item.Name.Equals(Folder.Name, StringComparison.OrdinalIgnoreCase) : !Item.Path.Equals(Folder.Path, StringComparison.OrdinalIgnoreCase))) { DriveList.Add(await DriveDataBase.CreateAsync(Folder, DriveType.Network)); } } catch (Exception ex) { LogTracer.Log(ex, $"Hide the device \"{Folder.Name}\" for error"); } } switch (PortalDeviceWatcher.Status) { case DeviceWatcherStatus.Created: case DeviceWatcherStatus.Aborted: case DeviceWatcherStatus.Stopped: { PortalDeviceWatcher.Start(); break; } } if (!NetworkDriveCheckTimer.IsEnabled) { NetworkDriveCheckTimer.Start(); } } } catch (Exception ex) { LogTracer.Log(ex, $"An exception was threw in {nameof(LoadDriveAsync)}"); } finally { _ = Interlocked.Exchange(ref LoadDriveLockResource, 0); } } }
private void FillDriveList(bool isRefreshing = false) { DriveInfo[] list; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { list = DriveInfo.GetDrives().Where(x => x.IsReady && (showAllDrives || (x.DriveType == DriveType.Removable && x.DriveFormat.StartsWith("FAT")))).ToArray(); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { list = DriveInfo.GetDrives().Where(x => x.IsReady && (showAllDrives || x.DriveType == DriveType.Removable || x.DriveType == DriveType.Fixed)).ToArray();//todo need to test } else//linux { list = DriveInfo.GetDrives().Where(x => x.IsReady && (showAllDrives || ((x.DriveType == DriveType.Removable || x.DriveType == DriveType.Fixed) && x.DriveFormat.Equals("msdos", StringComparison.InvariantCultureIgnoreCase) && x.Name.StartsWith("/media/", StringComparison.InvariantCultureIgnoreCase)))).ToArray(); } if (isRefreshing) { if (DriveList.Select(x => x.Name).SequenceEqual(list.Select(x => x.Name))) { return; } DriveList.Clear(); } //fill drive list and try to find drive with gdemu contents //look for GDEMU.ini file foreach (DriveInfo drive in list) { try { DriveList.Add(drive); if (SelectedDrive == null && File.Exists(Path.Combine(drive.RootDirectory.FullName, Constants.MenuConfigTextFile))) { SelectedDrive = drive; } } catch { } } //look for 01 folder if (SelectedDrive == null) { foreach (DriveInfo drive in list) { try { if (Directory.Exists(Path.Combine(drive.RootDirectory.FullName, "01"))) { SelectedDrive = drive; break; } } catch { } } } //look for /media mount if (SelectedDrive == null) { foreach (DriveInfo drive in list) { try { if (drive.Name.StartsWith("/media/", StringComparison.InvariantCultureIgnoreCase)) { SelectedDrive = drive; break; } } catch { } } } if (!DriveList.Any()) { return; } if (SelectedDrive == null) { SelectedDrive = DriveList.LastOrDefault(); } }