I would like to copy then delete the images from my digital cameras. There are 3 digital camera to connected to the pc via USB.
The pc sees these devices and add them as drives. I could copy the images from the drive, just I have to know whe do I copy the images from. Drive of the camera can change. It depends on which camera will be read and added earlier to the pc.
How should I indentify the cameras and copy the files from them.
Dealing with them in terms of them being removable disks is relatively easy, firstly finding the removable disks using DriveInfo
// Find removable disks
var drives = DriveInfo.GetDrives().Where(x => x.DriveType == DriveType.Removable && x.IsReady);
List<FileInfo[]> pictureHandles = new List<FileInfo[]>();
// Find all files of a certain type on each drive
foreach(var drive in drives)
{
pictureHandles.Add(
new DirectoryInfo(drive.Name)
.GetFiles("*.jpg",SearchOption.AllDirectories)
.ToArray());
}
// Do stuff with pictureHandles and identify
// which drive is which camera using System.Management
Identifying the cameras themselves will be more complicated. Only you will know exactly how to identify them, but I would suggest using WMI
via System.Management
. Here 's an article demonstrating the ManagementObjectSearcher
class, where you could look for the digital cameras on USB via certain properties, such as GUID
, Name
, etc. If I remember correctly, you would initialise it like so:
var searcher = new ManagementObjectSearcher("SELECT * FROM WIN32_USB");
And then iterate through,
foreach(var device in searcher.Get())
and check the properties:
if(device["Name"].ToString().Equals("My Digital Camera"))
Finally, when you have done whatever you wanted to do to the files, you can delete them like so:
foreach(FileInfo[] files in pictureHandles)
{
for (int i = 0; i < files.Length; i++)
{
try
{
File.Delete(files[i].FullName);
}
catch(Exception ex)
{
// Failed to delete
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.