I tried to make a simple Application which connects to my Digital Camera (which is detected as Portable Device) and Copy or Move the Files to my Harddisk (and put them in Directories, named by the DateTaken of the Photo). At first I tried to use the .NET FolderBrowserDialog
but it was soon clear, that it can’t handle Portable Devices and doesn’t even show them.
My approach was to use the shell32.dll
. It contains a BrowseForFolder
which is able to handle Portable Devices.
To use it, just a add Reference to the COM Component "Microsoft Shell Controls and Automation"
and include the Shell32
Namespace.
You can now call the BrowseForFolder with this Snippet:
Shell shell = new ShellClass(); Folder folder = shell.BrowseForFolder(( int )Hwnd, "Choose Folder" , 0, 0); if (folder == null ) // User cancelled |
(Hwnd is just a Handle to a Form)
Note that the Folder
is a COM-Object so you can’t use it like a .Net Directory.
You can get the Path out of it by Casting it to a FolderItem:
FolderItem fi = (folder as Folder3).Self; string path = fi.Path; |
This Path for my DigitalCamera was like: "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\\?\usb#vid_04a9&pid_3137#3b1feaedb9f64d6a9dd782783b9bcecd#{6ac27878-a6fa-4155-ba85-f98f491d4f33}\SID-{10001,,4066902016}\{00000003-0000-0000-0000-000000000000}\{00000004-0000-0000-0000-000000000000}\{00000005-0000-0000-0000-000000000000}"
Selecting a Folder on the Harddisk results in a normal Path like: "C:\Temp"
For the first case, I found no possibility to use this “Path” to get a .Net Directory. That’s why I had to stick with the Shell32.Folder for copying / moving.
You can now iterate through the Files with folder.Items()
but again, the FolderItem
Objects in this List are COM-Objects and I haven’t found any way to convert them to a .Net File.
I used another approach: The COM-Folder-Object has two handy functions: CopyHere
and MoveHere
.
So all I needed to do is to get a Source and a Target COM-Folder and call the CopyHere on the Target with the FolderItems from the Source Folder.
Here’s a Snippet for that:
public static void CopyFiles(Folder srcFolder, Folder dstFolder) { foreach (FolderItem currFolderItem in srcFolder.Items()) { dstFolder.CopyHere(currFolderItem, 0); } } |
CopyHere and MoveHere have two Parameters, the second is the Options. Here’s an Enum with the possible Options I found so far:
public enum CopyHereOptions { Default = 0, // No Options specified NoProgressDialog = 4, // Do not display a Progress DialogBox RenameTarget = 8, // Rename the Target File if a File exists at the Target Location with the same Name AutoYesToAll = 16, // Click "Yes to All" in any DialogBox displayed PreserveUndo = 64, // Preserve undo Information, if possible WildcardOnly = 128, // Perform the Operation only if a Wildcard Filename (*.*) is specified HideProgressFileNames = 256, // Display a Progress DialogBox but do not show the Filenames NoNewFolderConfirmation = 512, // Do not confirm the creation of a new Directory if needed NoUIonError = 1024, // Do not display a User Interface if an Error occurs DisableRecursion = 4096, // Disable Recursion NoGroupCopy = 9182 // Do not copy connected Files as a Group. Only copy the specified Files } |
In case you have a normal FolderPath and need it to be converted to a COM-Folder-Object, use this Snippet:
public static Folder Path2Folder( string path) { // Create Folder if needed Directory.CreateDirectory(path); // Create ShellObject Shell shell = new ShellClass(); // Convert to Shell Folder return shell.NameSpace(path); } |
That’s it, now all the Files are copied (or moved) from the Source to the Target. And after that, you can use a normal .Net Directory(Info) on your Target (which should be a normal FolderPath) to Access the Files with .Net.
#
#
Very useful.
Thanks
#
VERY VERY USEFUL!!!
I only need a way to obtain the path without doing all the “browse folder” manually. Do you know how we can do that?
The idea of your code is great, very mcgiver indeed.
Please answer if you improveed this post!
#
I have noticed you don’t monetize your website, don’t waste your traffic, you can earn extra cash every month because you’ve got high
quality content. If you want to know how to make extra $$$, search for:
Boorfe’s tips best adsense alternative
#
Excelent!! work perfect!