コード例 #1
0
        /// <summary>
        /// Initialize a new instance of the dialog using the given Controller.
        /// </summary>
        /// <param name="controller"></param>

        public SynchronizeDialog(Controller controller)
            : this()
        {
            this.controller = controller;

            this.manager             = new UsbManager();
            this.disks               = manager.GetAvailableDisks();
            this.playlists           = null;
            this.percentageCompleted = 0;

            this.selector.Loaded += new RoutedEventHandler(DoLoaded);
        }
コード例 #2
0
ファイル: UsbManager.cs プロジェクト: jeason0813/iTuner
        //========================================================================================
        // Methods
        //========================================================================================

        /// <summary>
        /// Gets a collection of all available USB disk drives currently mounted.
        /// </summary>
        /// <returns>
        /// A UsbDiskCollection containing the USB disk drives.
        /// </returns>

        public UsbDiskCollection GetAvailableDisks()
        {
            UsbDiskCollection disks = new UsbDiskCollection();

            using (var searcher = new ManagementObjectSearcher(
                       "select DeviceID, Model from Win32_DiskDrive where InterfaceType='USB'").Get())
            {
                // browse all USB WMI physical disks
                foreach (var o in searcher)
                {
                    var drive = (ManagementObject)o;
                    // associate physical disks with partitions
                    using (var partition = new ManagementObjectSearcher(
                               $"associators of {{Win32_DiskDrive.DeviceID='{drive["DeviceID"]}'}} where AssocClass = Win32_DiskDriveToDiskPartition")
                                           .First())
                    {
                        if (partition != null)
                        {
                            // associate partitions with logical disks (drive letter volumes)
                            using (var logical = new ManagementObjectSearcher(
                                       $"associators of {{Win32_DiskPartition.DeviceID='{partition["DeviceID"]}'}} where AssocClass = Win32_LogicalDiskToPartition")
                                                 .First())
                            {
                                if (logical != null)
                                {
                                    // finally find the logical disk entry to determine the volume name
                                    using (var volume = new ManagementObjectSearcher(
                                               $"select FreeSpace, Size, VolumeName from Win32_LogicalDisk where Name='{logical["Name"]}'")
                                                        .First())
                                    {
                                        var disk = new UsbDisk(logical["Name"].ToString())
                                        {
                                            Model     = drive["Model"].ToString(),
                                            Volume    = volume["VolumeName"].ToString(),
                                            FreeSpace = (ulong)volume["FreeSpace"],
                                            Size      = (ulong)volume["Size"]
                                        };

                                        disks.Add(disk);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(disks);
        }
コード例 #3
0
        /// <summary>
        /// Capture and process new data context changes.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        void DoDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            UsbDiskCollection disks = e.NewValue as UsbDiskCollection;

            devicesBox.ItemsSource = disks;

            if (disks != null)
            {
                disks.CollectionChanged += new NotifyCollectionChangedEventHandler(DoCollectionChanged);
                if (disks.Count > 0)
                {
                    devicesBox.SelectedIndex = 0;
                }
            }
        }
コード例 #4
0
        //========================================================================================
        // Methods
        //========================================================================================

        /// <summary>
        /// Gets a collection of all available USB disk drives currently mounted.
        /// </summary>
        /// <returns>
        /// A UsbDiskCollection containing the USB disk drives.
        /// </returns>

        public UsbDiskCollection GetAvailableDisks()
        {
            UsbDiskCollection disks = new UsbDiskCollection();

            // browse all USB WMI physical disks
            foreach (ManagementObject drive in
                     new ManagementObjectSearcher(
                         "select DeviceID, Model,Size from Win32_DiskDrive where InterfaceType='USB' or MediaType='External hard disk media'").Get())
            {
                // associate physical disks with partitions
                foreach (ManagementObject partition in new ManagementObjectSearcher(string.Format(
                                                                                        "associators of {{Win32_DiskDrive.DeviceID='{0}'}} where AssocClass = Win32_DiskDriveToDiskPartition",
                                                                                        drive["DeviceID"])).Get())
                {
                    try
                    {
                        if (partition != null)
                        {
                            // associate partitions with logical disks (drive letter volumes)
                            ManagementObject logical = new ManagementObjectSearcher(String.Format(
                                                                                        "associators of {{Win32_DiskPartition.DeviceID='{0}'}} where AssocClass = Win32_LogicalDiskToPartition",
                                                                                        partition["DeviceID"])).First();
                            if (logical != null)
                            {
                                // finally find the logical disk entry to determine the volume name
                                ManagementObject volume = new ManagementObjectSearcher(String.Format(
                                                                                           "select FreeSpace, Size, VolumeName, DriveType from Win32_LogicalDisk where Name='{0}'",
                                                                                           logical["Name"])).First();
                                UsbDisk disk = new UsbDisk(logical["Name"].ToString());
                                disk.Model     = drive["Model"].ToString();
                                disk.Volume    = volume["VolumeName"].ToString();
                                disk.FreeSpace = (ulong)volume["FreeSpace"];
                                disk.Size      = (ulong)volume["Size"];
                                disk.DiskSize  = (ulong)drive["Size"];
                                disk.DriveType = Drivetypeconvert(Int32.Parse(volume["DriveType"].ToString()));
                                disks.Add(disk);
                                //MessageBox.Show("HERE");
                            }
                        }
                    }
                    catch
                    { }
                }
            }

            return(disks);
        }
コード例 #5
0
        /// <summary>
        /// Release manages resources.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void DoClosed(object sender, EventArgs e)
        {
            if (percentageCompleted > 0)
            {
                controller.Librarian.ProgressChanged -= new ProgressChangedEventHandler(DoProgressChanged);
            }

            // must detach collection from WPF control otherwise will not dispose
            selector.DataContext = null;
            disks.Clear();
            disks = null;

            playlistBox.ItemsSource = null;

            playlists.Dispose();
            playlists = null;

            manager.Dispose();
            manager = null;

            controller = null;
        }
コード例 #6
0
        //========================================================================================
        // Methods
        //========================================================================================


        /// <summary>
        /// Gets a collection of all available USB disk drives currently mounted.
        /// </summary>
        /// <returns>
        /// A UsbDiskCollection containing the USB disk drives.
        /// </returns>

        public UsbDiskCollection GetAvailableVolumns()
        {
            UsbDiskCollection volumns = new UsbDiskCollection();

            // browse all USB WMI physical disks
            foreach (ManagementObject drive in
                     new ManagementObjectSearcher(
                         "select DeviceID,Model,Size,Index,MediaType,Size,InterfaceType from Win32_DiskDrive").Get())
            {
                try
                {
                    //if ((uint)drive["Index"] < 1) continue;
                    //if (drive["InterfaceType"].ToString() != "USB" && drive["InterfaceType"].ToString() != "SCSI") continue;
                    if (drive["Model"].ToString().Contains("APPLE SD"))
                    {
                        continue;
                    }
                    UsbDisk disk = new UsbDisk(drive["DeviceID"].ToString());

                    disk.Model     = drive["Model"].ToString();
                    disk.Index     = drive["Index"].ToString();
                    disk.DiskSize  = (ulong)drive["Size"];
                    disk.DriveType = drive["MediaType"].ToString();
                    disk.Size      = (ulong)drive["Size"];
                    // associate physical disks with partitions
                    foreach (ManagementObject partition in new ManagementObjectSearcher(string.Format(
                                                                                            "associators of {{Win32_DiskDrive.DeviceID='{0}'}} where AssocClass = Win32_DiskDriveToDiskPartition",
                                                                                            drive["DeviceID"])).Get())
                    {
                        try
                        {
                            if (partition != null)
                            {
                                // associate partitions with logical disks (drive letter volumes)
                                ManagementObject logical = new ManagementObjectSearcher(string.Format(
                                                                                            "associators of {{Win32_DiskPartition.DeviceID='{0}'}} where AssocClass = Win32_LogicalDiskToPartition",
                                                                                            partition["DeviceID"])).First();
                                if (logical != null)
                                {
                                    // finally find the logical disk entry to determine the volume name
                                    ManagementObject volume = new ManagementObjectSearcher(string.Format(
                                                                                               "select FreeSpace, Size, VolumeName, DriveType, FileSystem from Win32_LogicalDisk where Name='{0}'",
                                                                                               logical["Name"])).First();

                                    UsbDisk diskVolume = new UsbDisk(drive["DeviceID"].ToString());
                                    diskVolume.Model      = drive["Model"].ToString();
                                    diskVolume.Index      = drive["Index"].ToString();
                                    diskVolume.Volume     = logical["Name"].ToString();
                                    diskVolume.FileSystem = volume["FileSystem"].ToString();
                                    diskVolume.DiskSize   = (ulong)drive["Size"];
                                    diskVolume.DriveType  = drive["MediaType"].ToString();
                                    diskVolume.Size       = (ulong)volume["Size"];
                                    //diskVolume.DiskSize = (ulong)volume["Size"];
                                    diskVolume.FreeSpace = (ulong)volume["FreeSpace"];
                                    volumns.Add(diskVolume);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.ToString());
                            //disks.Add(new UsbDisk(ex.ToString()));
                        }
                    }
                    //if (!hasAdded) disks.Add(disk);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                    //MessageBox.Show(e.ToString());
                }
            }

            return(volumns);
        }