Пример #1
0
        public static bool CheckIfDriveAvailableToScan(PhysicalDriveData driveSelected)
        {
            bool bDriveAvailable = false;

            try
            {
                List<PhysicalDriveData> driveList = DriveInfoScanner.ScanDrives();

                foreach (var drive in driveList)
                {
                    if (drive.SerialNumber == driveSelected.SerialNumber)
                    {
                        bDriveAvailable = true;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return bDriveAvailable;
        }
Пример #2
0
        public static List<PhysicalDriveData> ScanDrives()
        {
            List<PhysicalDriveData> driveList = new List<PhysicalDriveData>();

            //var sb = new StringBuilder();

            var diskDriveManagement = new ManagementClass("Win32_DiskDrive");
            var diskDriveInstanceObjectCollection = diskDriveManagement.GetInstances();

            foreach (ManagementObject diskDriveInstanceObject in diskDriveInstanceObjectCollection)
            {
                bool bNoSerialNumber = false;

                PhysicalDriveData driveData = new PhysicalDriveData();

                //sb.AppendLine(DumpProperties(diskDriveInstanceObject, 0));

                driveData.Caption = GetProperty(diskDriveInstanceObject, "Caption");
                driveData.InterfaceType = GetProperty(diskDriveInstanceObject, "InterfaceType");
                driveData.Manufacturer = GetProperty(diskDriveInstanceObject, "Manufacturer");
                driveData.MediaType = GetProperty(diskDriveInstanceObject, "MediaType");
                driveData.Model = GetProperty(diskDriveInstanceObject, "Model");
                driveData.Partitions = GetProperty(diskDriveInstanceObject, "Partitions");
                driveData.SerialNumber = GetProperty(diskDriveInstanceObject, "SerialNumber");
                driveData.Size = GetProperty(diskDriveInstanceObject, "Size");

                Console.WriteLine(DumpProperties(diskDriveInstanceObject, 0));

                foreach (ManagementObject diskPartitionObject in diskDriveInstanceObject.GetRelated("Win32_DiskPartition"))
                {
                    Console.WriteLine(DumpProperties(diskPartitionObject, 1));

                    foreach (ManagementBaseObject logicalDiskObject in diskPartitionObject.GetRelated("Win32_LogicalDisk"))
                    {
                        DrivePartitionData partitionData = new DrivePartitionData();

                        partitionData.Caption = GetProperty(logicalDiskObject, "Caption");
                        partitionData.CreationClassName = GetProperty(logicalDiskObject, "CreationClassName");
                        partitionData.Description = GetProperty(logicalDiskObject, "Description");
                        partitionData.DeviceID = GetProperty(logicalDiskObject, "DeviceID");
                        partitionData.DriveType = GetProperty(logicalDiskObject, "DriveType");
                        partitionData.FileSystem = GetProperty(logicalDiskObject, "FileSystem");
                        partitionData.FreeSpace = GetProperty(logicalDiskObject, "FreeSpace");
                        partitionData.MediaType = GetProperty(logicalDiskObject, "MediaType");
                        partitionData.Name = GetProperty(logicalDiskObject, "VolumeName");
                        partitionData.Size = GetProperty(logicalDiskObject, "Size");
                        partitionData.VolumeSerialNumber = GetProperty(logicalDiskObject, "VolumeSerialNumber");

                        driveData.m_drivePartitions.Add(partitionData);
                        //sb.AppendLine(DumpProperties(logicalDiskObject, 2));

                        Console.WriteLine(DumpProperties(logicalDiskObject, 1));

                        
                        byte[] toBytes = Encoding.ASCII.GetBytes(driveData.SerialNumber);

                        if (toBytes[0] == 31 || string.IsNullOrEmpty(driveData.SerialNumber) == true || string.IsNullOrWhiteSpace(driveData.SerialNumber) == true)
                        {
                            // Use logical drive serialnumbers!

                            bNoSerialNumber = true;
                            // http://blogs.msdn.com/b/oldnewthing/archive/2004/11/10/255047.aspx
                            // Serial numbers are optional on USB devices!!! Why-O-Why!! Face-Palm!
                            driveData.SerialNumber += partitionData.VolumeSerialNumber;
                        }

                        //if (driveData.SerialNumber != "")
                        //{
                        //    Console.WriteLine("blah");
                        //}

                        //byte[] toBytes = Encoding.ASCII.GetBytes(driveData.SerialNumber);

                        //var toBytesStr = Encoding.ASCII.GetBytes(driveData.SerialNumber).ToString();

                        //byte[] toBytes2 = Encoding.ASCII.GetBytes("");
                    }
                }

                if ( bNoSerialNumber == true)
                {
                    driveData.SerialNumber += "_NOSERIAL";
                }

                driveList.Add(driveData);
            }

            //sb.AppendLine();
            //Console.WriteLine(sb.ToString());

            return driveList;
        }
Пример #3
0
        private void dataGridViewDrives_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
                return;

            m_dgvSelectedDrive = (PhysicalDriveData)dataGridViewDrives.Rows[e.RowIndex].Tag;
        }
Пример #4
0
        private void AddPhysicialDriveToDataGridView( DataGridView dgv, PhysicalDriveData driveData )
        {
            int nNewRow = dgv.Rows.Add();
            int nColCount = 0;

            if (driveData.DriveScanned == "0" || driveData.DriveScanned == "") // ugly!! 
                dgv.Rows[nNewRow].Cells[nColCount++].Value = DriveIndexer.Properties.Resources.Button_Close_icon24;
            else
                dgv.Rows[nNewRow].Cells[nColCount++].Value = DriveIndexer.Properties.Resources.Button_Check_icon24;

            dgv.Rows[nNewRow].Cells[nColCount++].Value = driveData.Name;
            dgv.Rows[nNewRow].Cells[nColCount++].Value = driveData.UserComment;
            dgv.Rows[nNewRow].Cells[nColCount++].Value = driveData.SerialNumber;
            dgv.Rows[nNewRow].Cells[nColCount++].Value = driveData.Model;
            dgv.Rows[nNewRow].Cells[nColCount++].Value = driveData.InterfaceType;
            dgv.Rows[nNewRow].Cells[nColCount++].Value = driveData.Partitions;
            dgv.Rows[nNewRow].Cells[nColCount++].Value = DriveInfoScanner.DriveSizeToGB(driveData.Size);
            dgv.Rows[nNewRow].Cells[nColCount++].Value = driveData.DriveScannedDate;

            // Assosiate driveData with this row.
            dgv.Rows[nNewRow].Tag = driveData;

            dgv.Rows[nNewRow].Height = 30;

            // make this RowNotInTableException readonly
            dgv.Rows[nNewRow].ReadOnly = true;

        }
Пример #5
0
        private void PopulateListView( )
        {
            m_physicalDriveList = DBHelper.ReadDriveList();

            dataGridViewDrives.Rows.Clear();

            foreach (var drive in m_physicalDriveList)
            {
                AddPhysicialDriveToDataGridView( dataGridViewDrives, drive );

                // set this drive as the selected drive!
                m_dgvSelectedDrive = drive;
            }

            // Unselect all rows in datagridview
            dataGridViewDrives.ClearSelection();
        }
Пример #6
0
        public static bool UpdateDriveScannedStatus(PhysicalDriveData data)
        {
            string sql = string.Format("Update PhysicalDrives set DriveScanned = 1, DriveScannedDate = '{0}' where DriveId = '{1}'", DateTime.Now.ToString("YYYY-MM-DD"), data.DriveId);

            return LocalSqllite.ExecSQLCommand(sql);
        }
Пример #7
0
        private static string WritePhysicalDriveToDatabase( PhysicalDriveData data )
        {
            StringBuilder sqlStatement = new StringBuilder();

            try
            {
                string driveId = CheckDriveIdExists(data);
                if (driveId == "")
                {
                    // insert statment
                    sqlStatement.AppendLine("INSERT OR IGNORE INTO PhysicalDrives ( Name, SerialNumber, Manufacturer, MediaType, Model, Partitions, Caption, InterfaceType, Size, UserComment ) VALUES ( ");
                    sqlStatement.AppendLine(string.Format("'{0}'", data.Name));
                    sqlStatement.AppendLine(string.Format(",'{0}'", data.SerialNumber));
                    sqlStatement.AppendLine(string.Format(",'{0}'", data.Manufacturer));
                    sqlStatement.AppendLine(string.Format(",'{0}'", data.MediaType));
                    sqlStatement.AppendLine(string.Format(",'{0}'", data.Model));
                    sqlStatement.AppendLine(string.Format(",'{0}'", data.Partitions));
                    sqlStatement.AppendLine(string.Format(",'{0}'", data.Caption));
                    sqlStatement.AppendLine(string.Format(",'{0}'", data.InterfaceType));
                    sqlStatement.AppendLine(string.Format(",'{0}'", data.Size));
                    sqlStatement.AppendLine(string.Format(",'{0}'", data.UserComment));
                    sqlStatement.AppendLine(");");
                }
                else
                {
                    // update statment
                    sqlStatement.AppendLine("UPDATE PhysicalDrives SET ");
                    sqlStatement.AppendLine(string.Format("Name = '{0}'", data.Name));
                    sqlStatement.AppendLine(string.Format(",SerialNumber = '{0}'", data.SerialNumber));
                    sqlStatement.AppendLine(string.Format(",Manufacturer = '{0}'", data.Manufacturer));
                    sqlStatement.AppendLine(string.Format(",MediaType = '{0}'", data.MediaType));
                    sqlStatement.AppendLine(string.Format(",Model = '{0}'", data.Model));
                    sqlStatement.AppendLine(string.Format(",Partitions = '{0}'", data.Partitions));
                    sqlStatement.AppendLine(string.Format(",Caption = '{0}'", data.Caption));
                    sqlStatement.AppendLine(string.Format(",InterfaceType = '{0}'", data.InterfaceType));
                    sqlStatement.AppendLine(string.Format(",Size = '{0}'", data.Size));
                    sqlStatement.AppendLine(string.Format(",UserComment = '{0}'", data.UserComment));
                    sqlStatement.AppendLine(string.Format("WHERE DriveId = '{0}'", driveId));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return sqlStatement.ToString();
        }
Пример #8
0
        public static string CheckDriveIdExists( PhysicalDriveData data )
        {
            string returnVal = "";
            string sql = string.Format("select DriveId from PhysicalDrives where SerialNumber = '{0}'", data.SerialNumber);

            returnVal = LocalSqllite.ExecSQLCommandScalar(sql);

            return returnVal;
        }
Пример #9
0
        public static void PopulatePhyicalDrive(PhysicalDriveData drive)
        {
            try
            {
                // check if we have already seen this drive! 
                string sql = WritePhysicalDriveToDatabase(drive);
                LocalSqllite.ExecSQLCommand(sql);

                string driveId = CheckDriveIdExists(drive);

                foreach (var drivePartition in drive.m_drivePartitions)
                {
                    sql = WritePartitionDataToDatabase(driveId, drivePartition);
                    LocalSqllite.ExecSQLCommand(sql);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }
Пример #10
0
        public static List<DrivePartitionData> ReadDrivePartitionList( PhysicalDriveData drive )
        {
            List<DrivePartitionData> list = new List<DrivePartitionData>();

            try
            {
                string sql = "select PartitionId, DriveId, VolumeSerialNumber, Name, Caption, Description, DeviceID, DriveType, FileSystem, FreeSpace, Size, UserComment FROM PhysicalDrivePartitions where DriveId = " + drive.DriveId;
                SQLiteCommand command = new SQLiteCommand(sql, LocalSqllite.m_sqlLiteConnection);
                SQLiteDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    DrivePartitionData data = new DrivePartitionData();
                    data.PartitionId = reader["PartitionId"].ToString();
                    data.DriveId = reader["DriveId"].ToString();
                    data.VolumeSerialNumber = reader["VolumeSerialNumber"].ToString();
                    data.Name = reader["Name"].ToString();
                    data.Description = reader["Description"].ToString();
                    data.DeviceID = reader["DeviceID"].ToString();
                    data.DriveType = reader["DriveType"].ToString();
                    data.FileSystem = reader["FileSystem"].ToString();
                    data.FreeSpace = reader["FreeSpace"].ToString();
                    data.Size = reader["Size"].ToString();
                    data.UserComment = reader["UserComment"].ToString();

                    data.PhysicalDrive = drive;

                    list.Add(data);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return list;
        }
Пример #11
0
        public static List<PhysicalDriveData> ReadDriveList()
        {
            List<PhysicalDriveData> list = new List<PhysicalDriveData>();

            try
            {
                string sql = "select DriveId, Name, SerialNumber, Manufacturer, MediaType, Model, Partitions, Caption, InterfaceType, Size, UserComment, DriveScanned, DriveScannedDate FROM PhysicalDrives";
                SQLiteCommand command = new SQLiteCommand(sql, LocalSqllite.m_sqlLiteConnection);
                SQLiteDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    PhysicalDriveData data = new PhysicalDriveData();
                    data.DriveId = reader["DriveId"].ToString();
                    data.Name = reader["Name"].ToString();
                    data.SerialNumber = reader["SerialNumber"].ToString();
                    data.Manufacturer = reader["Manufacturer"].ToString();
                    data.MediaType = reader["MediaType"].ToString();
                    data.Model = reader["Model"].ToString();
                    data.Partitions = reader["Partitions"].ToString();
                    data.Caption = reader["Caption"].ToString();
                    data.InterfaceType = reader["InterfaceType"].ToString();
                    data.Size = reader["Size"].ToString();
                    data.UserComment = reader["UserComment"].ToString();
                    data.DriveScanned = reader["DriveScanned"].ToString();
                    data.DriveScannedDate = reader["DriveScannedDate"].ToString();

                    data.m_drivePartitions = ReadDrivePartitionList(data);
                    list.Add(data);

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return list;
        }