public static bool CheckIfDriveAvailableToScan(DrivePartitionData logicalDrive) { bool bDriveAvailable = false; try { List<PhysicalDriveData> driveList = DriveInfoScanner.ScanDrives(); foreach (var drive in driveList) { foreach (var drivePartition in drive.m_drivePartitions) { if (drivePartition.VolumeSerialNumber == logicalDrive.VolumeSerialNumber) { bDriveAvailable = true; break; } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } //string[] availableDriveLabels = System.Environment.GetLogicalDrives(); //if (availableDriveLabels.Contains( string.Format(@"{0}\",logicalDrive.Name) ) == true) // bDriveAvailable = true; return bDriveAvailable; }
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; }
public static string GetCurrentDriveLetterForDrive(DrivePartitionData driveInfo ) { string currentDriveLetter = ""; try { List<PhysicalDriveData> driveList = DriveInfoScanner.ScanDrives(); foreach (var drive in driveList) { foreach (var drivePartition in drive.m_drivePartitions) { if (drivePartition.VolumeSerialNumber == driveInfo.VolumeSerialNumber) { currentDriveLetter = drivePartition.Name; break; } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } return currentDriveLetter; }
public static void ScanDrive(DrivePartitionData logicalDrive) { try { m_logicalDrive = logicalDrive; if (CheckIfDriveAvailableToScan(logicalDrive) == false) { OutputMessage(string.Format("Logical Drive: {0} not available to scan", logicalDrive.Name)); return; } logicalDrive.Name = GetCurrentDriveLetterForDrive(logicalDrive); OutputMessage(string.Format("Starting scan: {0}", logicalDrive.Name)); //SetupExtensionWhiteList(); System.IO.DriveInfo di = new System.IO.DriveInfo(logicalDrive.DeviceID); // Here we skip the drive if it is not ready to be read. This // is not necessarily the appropriate action in all scenarios. if (!di.IsReady) { OutputMessage(string.Format("The drive: {0} could not be read", di.Name)); Console.WriteLine("The drive {0} could not be read", di.Name); return; } System.IO.DirectoryInfo rootDir = di.RootDirectory; WalkDirectoryTree(rootDir); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public static bool WriteFileToDatabase(DrivePartitionData data, System.IO.FileInfo fi, FileTypeData extensionInfo, IFileExplorerUIManager ui) { bool bSuccessInsert = false; try { string dirName = GetDirectoryName(fi); StringBuilder sqlStatement = new StringBuilder(); string fileExists = CheckFileExists(data, fi); if (fileExists == "") { // if (ui != null) // ui.OutputFileScanned(string.Format("Hashing File: {0} (Size: {1})", fi.Name, fi.Length)); string hash = ""; // GetFileHash(fi.FullName); // insert statment sqlStatement.AppendLine("INSERT OR IGNORE INTO FileIndex ( DriveId, PartitionId, FileName, FileExtension, FilePath, FileSize, FileTypeId, FileGroupId, FileTag, FileHash, UserComment ) VALUES ( "); sqlStatement.AppendLine(string.Format("'{0}'", data.PhysicalDrive.DriveId)); sqlStatement.AppendLine(string.Format(",'{0}'", data.PartitionId)); sqlStatement.AppendLine(string.Format(",'{0}'", fi.Name.Replace("'","''"))); sqlStatement.AppendLine(string.Format(",'{0}'", fi.Extension)); sqlStatement.AppendLine(string.Format(",'{0}'", dirName.Replace("'", "''"))); sqlStatement.AppendLine(string.Format(",'{0}'", fi.Length)); sqlStatement.AppendLine(string.Format(",'{0}'", extensionInfo.m_fileTypeId)); sqlStatement.AppendLine(string.Format(",'{0}'", extensionInfo.m_fileGroupId)); sqlStatement.AppendLine(string.Format(",'{0}'", "")); sqlStatement.AppendLine(string.Format(",'{0}'", hash)); sqlStatement.AppendLine(string.Format(",'{0}'", "")); sqlStatement.AppendLine(");"); bSuccessInsert = LocalSqllite.ExecSQLCommand(sqlStatement.ToString()); //Console.WriteLine(string.Format("{0}: {1}", hash, fi.FullName)); if (ui != null) ui.OutputFileScanned(string.Format("Indexing File: {0}", fi.Name)); } else { //Console.WriteLine(string.Format("Skipping File (Already Indexed): {0}", fi.Name)); if (ui != null) ui.OutputFileScanned(string.Format("Skipping File (Already Indexed): {0}", fi.FullName)); } //else //{ // // update statment // sqlStatement.AppendLine("UPDATE PhysicalDrivePartitions SET "); // sqlStatement.AppendLine(string.Format("VolumeSerialNumber = '{0}'", data.VolumeSerialNumber)); // sqlStatement.AppendLine(string.Format("Name = '{0}'", data.Name)); // sqlStatement.AppendLine(string.Format("Caption = '{0}'", data.Caption)); // sqlStatement.AppendLine(string.Format("Description = '{0}'", data.Description)); // sqlStatement.AppendLine(string.Format("DeviceID = '{0}'", data.DeviceID)); // sqlStatement.AppendLine(string.Format("DriveType = '{0}'", data.DriveType)); // sqlStatement.AppendLine(string.Format("FileSystem = '{0}'", data.FileSystem)); // sqlStatement.AppendLine(string.Format("FreeSpace = '{0}'", data.FreeSpace)); // sqlStatement.AppendLine(string.Format("Size = '{0}'", data.Size)); // sqlStatement.AppendLine(string.Format("UserComment = '{0}'", data.UserComment)); // sqlStatement.AppendLine(string.Format("WHERE DriveId = '{0}'", partitionId)); //} } catch (Exception ex) { Console.WriteLine(ex.Message); } return bSuccessInsert; }
private static string CheckFileExists(DrivePartitionData data, System.IO.FileInfo fi) { string returnVal = ""; string dirName = GetDirectoryName(fi); string sql = string.Format("select FileId from FileIndex where FileName = '{0}' and FilePath = '{1}' and DriveId = '{2}' and PartitionId = '{3}'", fi.Name.Replace("'", "''"), dirName.Replace("'", "''"), data.PhysicalDrive.DriveId, data.PartitionId); returnVal = LocalSqllite.ExecSQLCommandScalar(sql); return returnVal; }
private static string WritePartitionDataToDatabase(string driveId, DrivePartitionData data) { StringBuilder sqlStatement = new StringBuilder(); try { string partitionId = CheckPartitionIdExists(driveId, data); if (partitionId == "") { // insert statment sqlStatement.AppendLine("INSERT OR IGNORE INTO PhysicalDrivePartitions ( DriveId, VolumeSerialNumber, Name, Caption, Description, DeviceID, DriveType, FileSystem, FreeSpace, Size, UserComment ) VALUES ( "); sqlStatement.AppendLine(string.Format("'{0}'", driveId)); sqlStatement.AppendLine(string.Format(",'{0}'", data.VolumeSerialNumber)); sqlStatement.AppendLine(string.Format(",'{0}'", data.Name)); sqlStatement.AppendLine(string.Format(",'{0}'", data.Caption)); sqlStatement.AppendLine(string.Format(",'{0}'", data.Description)); sqlStatement.AppendLine(string.Format(",'{0}'", data.DeviceID)); sqlStatement.AppendLine(string.Format(",'{0}'", data.DriveType)); sqlStatement.AppendLine(string.Format(",'{0}'", data.FileSystem)); sqlStatement.AppendLine(string.Format(",'{0}'", data.FreeSpace)); sqlStatement.AppendLine(string.Format(",'{0}'", data.Size)); sqlStatement.AppendLine(string.Format(",'{0}'", data.UserComment)); sqlStatement.AppendLine(");"); } else { //// update statment //sqlStatement.AppendLine("UPDATE PhysicalDrivePartitions SET "); //sqlStatement.AppendLine(string.Format("VolumeSerialNumber = '{0}'", data.VolumeSerialNumber)); //sqlStatement.AppendLine(string.Format(",Name = '{0}'", data.Name)); //sqlStatement.AppendLine(string.Format(",Caption = '{0}'", data.Caption)); //sqlStatement.AppendLine(string.Format(",Description = '{0}'", data.Description)); //sqlStatement.AppendLine(string.Format(",DeviceID = '{0}'", data.DeviceID)); //sqlStatement.AppendLine(string.Format(",DriveType = '{0}'", data.DriveType)); //sqlStatement.AppendLine(string.Format(",FileSystem = '{0}'", data.FileSystem)); //sqlStatement.AppendLine(string.Format(",FreeSpace = '{0}'", data.FreeSpace)); //sqlStatement.AppendLine(string.Format(",Size = '{0}'", data.Size)); //sqlStatement.AppendLine(string.Format(",UserComment = '{0}'", data.UserComment)); //sqlStatement.AppendLine(string.Format("WHERE DriveId = '{0}'", partitionId)); } } catch (Exception ex) { Console.WriteLine(ex.Message); } return sqlStatement.ToString(); }
private static string CheckPartitionIdExists( string driveId, DrivePartitionData data ) { string returnVal = ""; string sql = string.Format("select PartitionId from PhysicalDrivePartitions where VolumeSerialNumber = '{0}' and DriveId = '{1}'", data.VolumeSerialNumber, driveId); returnVal = LocalSqllite.ExecSQLCommandScalar(sql); return returnVal; }
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; }