Drag and Drop from ListView to Explorer [duplicate] - c#

This question already has answers here:
Drag Drop from .NET application to Explorer
(2 answers)
Closed 7 years ago.
My goal is to able to drag an item from a ListView to Explorer. The way I'm trying to accomplish this is somewhat different though. From searching Google and around this site, I couldn't really find anything that would help me or pertained closely enough to what I'm trying to do. I can barely comprehend some of the examples because I don't understand enough about these drag and drop operations.
Specifically, the control that I'm trying to drag items from is a TreeListView from the ObjectListView library. The control is populated with nodes that represent the structure inside a archive-like compressed file. Entries are parsed into two different objects representing a file and a directory. Both of these models inherit an interface. When extracting these files normally, I use a Form that I instantiate with a collection of nodes and the target path that they will be extracted to as the parameters. It then takes care of extraction. This is so that I can show the overall and singular progress of each file.
Subscribing to the ItemDrag event, I know that I need to call DoDragDrop. When doing this, I need to be able to instantiate the extraction form only after the drop (when the user releases the mouse) into Explorer, and also be able to retrieve the path in which the items were dragged.
For some reason, this seems a lot more complicated than it should be. Any sort of advice or suggestions would be very helpful.

I'm not sure what format Explorer expects in the drag object, but this process might work for you.
When you begin the drag, pre-extract the selected files to some temp
directory
Build a DataObject which contains the paths of the files you have extracted in the format expected by Explorer
Use your DataObject in the DoDragDrop call
When the user drops on Explorer, the files should get copied from the temp directory to the drop target
Track all of your extracted files and cleanup when the application exists (id desired)
Hope this helps.
Edit (after some research):
I believe if you use
new DataObject(DataFormats.FileDrop, files)
where files is a string[] of the filenames to be copied (the ones you put in the temp directory), you shouold have the correct DataObject for implementation.
Haven't had a chance to write a prototype yet to test. Good Luck!

Related

Retrieving the recommended programs for a file extension

I cannot find a solution anywhere to this problem. I've only seen bits and pieces of code that barely work to get some exe files associated in the registry to that particular extension.
What I want is the exact list the Windows Explorer context menu will give you if you go to the Open With item. If I have to do it with p/invoke that is fine, I just want the exact list that explorer would show. That would include the friendly name as well as the path to associated program.
The list I want to retrieve for a pdf:
The list I want to retrieve for a png:
It may be worth noting that Microsoft calls it the "Recommended Programs" if you attempt to choose by clicking "Choose default program..."
So how is that dialog populated? Because it must be using the same method for the Open With context menu.
There are some popular answers to this question on SO, but most of them only half work.
Example Answer #1
Example Answer #2
If the AssocQueryString function returned multiple results it'd be close to an answer, however I believe it can only get the actual program associated with it.

Get Outlook folders icons

I am building an add-in which requires selection of multiple outlook folders. For that purpose I created selection dialog that lists folders and allows user to select one or more by checking the check boxes next to tree view items.
I was hoping that somehow I could read/get the outlook folder icons from my add in code which is written in C# (any solution that works with outlook object model would do).
I was trying hard around MAPIFolder.GetCustomIcon but it returns null for all folders and when you read the documentation it is clear that it is not meant for this i.e. it returns value only if folder has custom icon and if it is not any of default folders.
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.mapifolder.getcustomicon%28v=office.14%29.aspx
Thanks for reading.
You'll have to use an icon editing tool (like Axialis Icon Workshop) that can extract icon and image resources from related Outlook .dll and .exe files. It's a real mess to figure out what icon is where there as they exist in small and large quantities in very many different files.

WPF file to file drag and drop

I would like to have a function for my application such that dragging any file in Windows Explorer to a file having the correct format for my application adds that first file to the second, like with WinZip, 7-zip etc. Example, if I drag a file onto another zip file, it initiates the default application for Zip (in my case 7-zip) and adds it to the zip archive I drop it onto.
I've tried searching for a way to do this on Google, but I don't know what this type of function would be called or the correct keywords I should use. Referencing drag and drop, shell extensions etc. all points me to dragging a file from the Shell into my application or vice versa which I know how to do. Can anyone point me in the direction of what I should be searching for, or even better has some example code/tutorial on how to achieve this?
Well, searching for Shell extensions was correct. There is a github project that makes it easy to create shell extensions in .Net: https://github.com/dwmkerr/sharpshell
One of them is a drop handler, and that is what you're looking for if I understand your question right. There are some tutorials on how to use SharpShell on CodeProject, this one specific for the drop handler: http://www.codeproject.com/Articles/529515/NET-Shell-Extensions-Shell-Drop-Handlers

Creating a custom file property in C# [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Can one add custom properties to NTFS folders?
I am trying to write a standalone executable in C# that attaches a value to a text file (without changing the content of the file) which can be read later.
I have looked into the following solutions but none of them quite work:
Shell32.dll: Only appears to allow reading the properties. I could not find any information on how to use Shell32 to write properties.
DSOFile: App needs to be standalone and I am also concerned about future OS compatibility.
NTFS Alternate Data Streams (ADS): Many tools will not copy this information if the file is moved or copied and I am concerned about future compatibility.
Is there another way (COM, Etc..) to attach a custom property to a text file in .NET?
The only three ways I can think of:
ADS - however you've already dismissed this.
Add it to the file name.
Add it to the beginning or end of the file.
If it's an application controlled file that may be moved around then your best bet is number two. Of course, this means that the file may "lose" that info in a renaming operation.
The third option is a bit iffy. If you can control how the file is viewed, then you can simply give it a different extension and write your data to the top of it. When it needs to be opened you can "extract" the real file and open accordingly. I think this is WAY more complicated... but it will contain your data and be less likely to be jacked with.

How can you detect when files are dropped into Windows Explorer?

I have a WinForms app that has a TreeView. The user can drag files from WindowsExplorer to the TreeView, and then they can drag the files back into WindowsExplorer which in affect copies the files to wherever the files were dropped. What I'm trying to do is, if the files already exist in the directory where the files are being dropped, I want to rename the files/folders being copied in ahead of time, so that there's no collision.
Here's how I'm copying files into WindowsExplorer. On the treeView's ItemDrag, I loop through the nodes of the selected node, and then package that into an array. Then, I use this code:
var dataObject = new DataObject(DataFormats.FileDrop, files.ToArray());
dataObject.SetData(DataFormats.StringFormat, dataObject);
DoDragDrop(dataObject, DragDropEffects.Copy);
This works well, but once it ships off to Windows Explorer, it's out of my hands. How can I find out when and where the files are being copied TO and intercept that to make changes? Is this possible?
Explorer Drag & Drop is an excellent article doing what you are trying to achieve.
EDIT2: It seems that there's a C++ article available for the same on CodeProject. But I was unable to find a way of how to do it using C#.
AFAIK, there is no way to know drop target (in your case destination folder). You can look into CFSTR_FILENAMEMAP shell clipboard format, but still in this case you can only provide name mappings before (or in process) of drag-n-drop.
Also note, that default DataObject in .net has limited shell support. So if you need to use mentioned above format, you need to write your own IDataObject implementation (or take someone's implementation, good example with lot shell drag-n-drop related stuff can be found here)
Instead of putting the file names into the dataobject, create a temporary file with a unique/easily distinguishable name and place that file name into the data object's drop list instead (That file could be empty or contain some information you might need). Use a FileSystemWatcher (watching an entire drive) to detect the drop (set the filter to the temporary file name, set IncludeSubDirectories to true, and set Path to root directory of drive to watch.) Initiate the DoDragDrop. Once the unique/easily distinguishable file is dropped, the FileSystemWatcher can tell you where it was dropped and you can do whatever you need to do (e.g. delete the dropped temporary file and replace with the ones you originally wanted to drop. It is a far-from-perfect solution but might help. Better still, it might give someone an idea to come up with a better one!)
One downside is that you don't really know in which drive someone might drop the file and you may have to set up a watcher for several drives. And if you miss a drive (or a network path) then problems .....! Remember to dispose of the watchers after the drop.
There has to be a better way though. e.g. consider when you drag a file from a zip folder. The file is only extracted after the drop.
i don't think that is possible.

Categories