Following my scenario.
I got an Application which loads a Filestructure (Folders, Files) from a Database into a WPF ListView. Now I'd like to grab a file from this ListView, drag it over my Desktop (or some open explorer window) and drop it there. Basic Drag and Drop, nothing fancy. This sounds like a "standard" function for a windows application - but google won't help.
So how can I achieve this? Interops?
Thanks
Edit: Thanks for the solution, I still had to do some googling. Here's my complete solution.
DragDrop.DoDragDrop can do this as long as you pass it an appropriate DataObject.
First copy the files somewhere. You can use System.IO.Path.GetTempPath() if you don't have anywhere better.
Next create a string array containing the full paths to the files and do the following:
string[] paths = ...;
DragDrop.DoDragDrop(this, new DataObject(DataFormats.FileDrop, paths),
DragDropEffects.Copy);
It is actually possible to do this without pre-copying the files but that gets into some complicated IDataObject interactions, so unless your files are potentially very large and aren't already in the filesystem I would try this method first.
Related
INTRO:
Good day dear coders! I took a look around and wasn't quite able to find answer to my simple question, tho some questions do answer my question, however they seem to be advanced for me to fathom so I'm printing here my simple situation.
QUESTION:
I want to change BackgroundImage or Image of PictureBox and thats how I am doing this:
PictureBox.Image = new Bitmap(#"C:\Users\Ailayna\documents\visual studio 2012\Projects\FormCritterTalking\FormCritterTalking\Character Pictures\CharacterNormal.png");
This picture located in one of my project folders and some how writing whole path to the picture makes no logical sense to me, since I have included all needed pictures to the project, in specific project's folder and I wonder is there a more efficient way of changing picture rather than specifying the whole path of where picture is located?
Is there a way to directly access my folder where my project pictures located using code, like for example: PictureBox.Image = FolderName.PictureName;
I would like to know how are you guys doing this in more efficient and neat way? And another thing is, do I always have to say "new Bitmap"? Can it be something else?
Is there a way to directly access my folder where my project pictures
located using code, like for example: PictureBox.Image =
FolderName.PictureName;
Yes, there is. It's called resources.
In your project select properties then chose resources. Add image.
Access it:
yourProjectName.Properties.Resources.imageName
However I'd recommend to use streams to access images. This is the right way, especially in case of bitmaps. Do not forget to dispose it afterwards. See my answer here how to do this.
I am not aware of any PictureBox control in WPF but if you are looking for the same in WPF You can do it like this in XAML:
<Image Source="/MyProject.UI.Common;component/Images/Cut.png"/>
Here your Image is located inside "Images" folder.
Disclaimer: Since you're loading a picture directly off the hard drive (and I'm assuming it's working) I'm guessing we're talking about a WinForms project.
There are a number of options you can choose from.
The simplest of which is probably getting the current application assembly folder and using that:
How do I get the path of the assembly the code is in?
var dir = AppDomain.CurrentDomain.BaseDirectory;
The images should be copied along with the executable. Then you can either use the code above to get the current directory OR you can try and rely on a relative path (from where the executable is located to the images).
Another option is to add the pictures as resources - these will be part of your application, but the procedure of reading them is a bit more complex.
I'am working on my small project: Crypt container. To unlock it - just plug-in specifig usb flash-drive and unplug it to lock container.
So, GUI of program is very simple - just ListView. If you want to add files into continer - drag'n'drop items to them.
But i have a problem - when user want to decrypt his file, he drag them in container and drop to desktop (or specific folder). And my task is to know, where user wants to put a file. If he drag file to folder - i should unpack it to folder, if he drag it to flash-drive - i should unpuck it to flash-drive.
Yes, i found one solution of my problem: http://www.codeproject.com/KB/files/DragNDrop.aspx , but i realy not understand that mechanism with tempDirectoryWatcher and Hashtables.
Maybe somebody know easy way to get path to object (folder, drive, etc.) under cursor?
Your data object should expose FILEGROUPDESCRIPTOR and FILECONTENTS. The user might drop onto something other than a folder, like an email message, or into a virtual folder like an FTP site or a ZIP folder
I know this is normaly implemented with a temp file but this wont work here since we are dealing with pretty large files stored in a database.
so far my idea is to create a small temp file that has a unique name and do a FileSystemWatcher monitoring the drop to get the path. but this doesnt seem optimal eighter.
are there any other ideas you guys could point me to?
maybe with virtual files? if so - how?
will be greatful for any hint.
thanks.
What you're looking for is a fully functional implementation of IDataObject. See http://www.codeproject.com/KB/dotnet/DataObjectEx.aspx for an example of how this can be achieved. This implementation allows you to provide a MemoryStream which contains the actual file drag/drop contents.
I'm trying to achieve functionality similar to winzip/winrar, etc. I have a Treeview that displays the contents of a package (System.IO.Packaging). I want to be able to drag and drop a file or folder from the TreeView onto an explorer window or the desktop, etc. My problem is that I have to call DoDragDrop before I know if the object was actually even dropped. That means to create the DataObject with the FileDrop type, I must extract those contents of the package out to a temporary area and then provide that path to the DataObject before calling DoDragDrop. If the user doesn't drop on a capable container or cancels the drop, the overhead of extracting those contents is wasted. I've noticed that winzip does not actually create the temporary file until the drop occurs on a valid target. I've checked the DataFormats provided by the WinZip drop and they are normal FileDrop, FileNameW, FileName and Shell IDList Array. The first three simply hold a string to the temporary location that winzip extracted that file to. I'm not sure what the last one does.
Long story short, I want to be able to avoid extracting the contents until I know the drop location was valid. Is there a callback to figure out the drop location? Any suggestions would be extremely helpful.
System.Windows.DragDropEffects de = DragDrop.DoDragDrop(treeview1, dataObj, System.Windows.DragDropEffects.Move);
I've tried this with an application similar to an FTP server - I wanted to start downloading only after the user actually dropped the item. Unfortunately I found no way to do this using managed code only.
The way WinZip probably does it is by receiving COM callbacks (please forgive me if I'm using the wrong words here) and you'd have to create a managed wrapper around the native COM object in order to receive such callbacks yourself.
It's certainly possible, but I gave up and embedded a FolderTreeView thingie in my application to catch the drop events myself :/
I have a query about drag-drop in C# using .NET.
My issue is with remote files. I have a list of remote files which the user can drag into an explorer window (desktop, etc). When such a drag occurs I want to be able to download the file and write it to the drop location.
The normal method of dragging files:
private void StartDragDrop(string FileToDrag)
{
MyControl.DoDragDrop(new DataObject(DataFormats.FileDrop, FileToDrag), DragDropEffects.Copy);
}
...does not suit my needs as I will not have the file data to populate the drag-drop object until after the DROP.
I have seen this functionality in many FTP clients and such.
Thanks in advance wizards.
It appears there is no logically straight forward method to do such a thing.
But there is a work around (with a substantial amount of work for such basic functionaliy) that can be found at codeproject.
If anyone finds a tidy way of doing it, let me know, I'd be intrigued.