{"id":898,"date":"2013-04-29T20:50:49","date_gmt":"2013-04-29T19:50:49","guid":{"rendered":"https:\/\/minibrain.wordpress.com\/?p=898"},"modified":"2013-04-29T20:50:49","modified_gmt":"2013-04-29T19:50:49","slug":"dragdrop-filedrop-aus-eigener-applikation-heraus","status":"publish","type":"post","link":"https:\/\/abramowitsch.de\/blog\/?p=898","title":{"rendered":"Drag&#038;Drop: FileDrop aus eigener Applikation heraus"},"content":{"rendered":"<p>M\u00f6chte man per Drag&amp;Drop Dateien aus dem Windows Explorer in die eigene Applikation hereinziehen, so ist das kein Problem:<\/p>\n<ul>\n<li>Platzieren eines Controls, auf dem die Datei(en) per Drag&amp;Drop gezogen werden sollen<\/li>\n<li>Eigenschaft \u201cAllowDrop\u201d des Controls auf \u201cTrue\u201d setzen:<\/li>\n<\/ul>\n<blockquote>\n<p><a href=\"http:\/\/abramowitsch.de\/blog\/wp-content\/uploads\/2013\/04\/image9.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top:0;border-right:0;background-image:none;border-bottom:0;padding-top:0;padding-left:0;margin:0;border-left:0;display:inline;padding-right:0;\" border=\"0\" alt=\"image\" src=\"http:\/\/abramowitsch.de\/blog\/wp-content\/uploads\/2013\/04\/image_thumb9.png\" width=\"244\" height=\"234\" \/><\/a><\/p>\n<\/blockquote>\n<ul>\n<li>Registrieren auf die Events DragEnter und DragDrop:<\/li>\n<\/ul>\n<blockquote>\n<p><a href=\"http:\/\/abramowitsch.de\/blog\/wp-content\/uploads\/2013\/04\/image10.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top:0;border-right:0;background-image:none;border-bottom:0;padding-top:0;padding-left:0;margin:0;border-left:0;display:inline;padding-right:0;\" border=\"0\" alt=\"image\" src=\"http:\/\/abramowitsch.de\/blog\/wp-content\/uploads\/2013\/04\/image_thumb10.png\" width=\"244\" height=\"174\" \/><\/a><\/p>\n<\/blockquote>\n<ul>\n<li>Im EventHandler f\u00fcr DragEnter pr\u00fcfen, ob es sich im Dateien und den Mauszeiger anpassen handelt:<\/li>\n<\/ul>\n<blockquote>\n<p><font face=\"Consolas\">if (e.Data.GetDataPresent(&quot;FileDrop&quot;))       <br \/>{        <br \/>&#160;&#160;&#160; e.Effect = DragDropEffects.Copy;        <br \/>}<\/font><\/p>\n<\/blockquote>\n<ul>\n<li>Im EventHandler f\u00fcr DragDrop die Datei auslesen (und in einer TextBox darstellen)<\/li>\n<\/ul>\n<blockquote>\n<p><font face=\"Consolas\">string[] files = e.Data.GetData(&quot;FileDrop&quot;) as string[];       <br \/>if (files == null) return;<\/font><\/p>\n<p><font face=\"Consolas\">_txtContent.Text = File.ReadAllText(files[0]);<\/font><\/p>\n<\/blockquote>\n<p>Das Resultat kann dann so aussehen:<\/p>\n<p><a href=\"http:\/\/abramowitsch.de\/blog\/wp-content\/uploads\/2013\/04\/image11.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top:0;border-right:0;background-image:none;border-bottom:0;padding-top:0;padding-left:0;margin:0;border-left:0;display:inline;padding-right:0;\" border=\"0\" alt=\"image\" src=\"http:\/\/abramowitsch.de\/blog\/wp-content\/uploads\/2013\/04\/image_thumb11.png\" width=\"244\" height=\"227\" \/><\/a><\/p>\n<p>Anders herum ist es jedoch etwas schwieriger: Wie kann ich Daten beispielsweise aus einer TextBox per Drag&amp;Drop in eine Datei in den Windows Explorer droppen?<\/p>\n<p>Dazu muss man sich eines kleinen Tricks behelfen: Man muss ein FileDrop initiieren und die zu droppenden Daten in eine tempor\u00e4re Datei kopieren. Der Dateiname der tempor\u00e4ren Datei ist dann auch der Dateiname des Drop Targets. Hier ist eine kleine Schritt-f\u00fcr-Schritt Anleitung:<\/p>\n<ul>\n<li>Registrieren auf das MouseDown Event als Initiator f\u00fcr die Drag&amp;Drop Aktion:<\/li>\n<\/ul>\n<blockquote>\n<p><a href=\"http:\/\/abramowitsch.de\/blog\/wp-content\/uploads\/2013\/04\/image12.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top:0;border-right:0;background-image:none;border-bottom:0;padding-top:0;padding-left:0;margin:0;border-left:0;display:inline;padding-right:0;\" border=\"0\" alt=\"image\" src=\"http:\/\/abramowitsch.de\/blog\/wp-content\/uploads\/2013\/04\/image_thumb12.png\" width=\"244\" height=\"149\" \/><\/a><\/p>\n<\/blockquote>\n<ul>\n<li>Anlegen einer tempor\u00e4ren Datei mit dem Inhalten, die in die zu droppende Datei geschrieben werden sollen. Wichtig: Der Dateiname ist auch der Dateiname der dort erstellt wird, wo die Datei im Explorer gedropped wird:<\/li>\n<\/ul>\n<blockquote>\n<p><font face=\"Consolas\">\/\/ Create temporary file name       <br \/>string tempFile = Path.Combine(Path.GetTempPath(), &quot;LoremIpsum.txt&quot;);<\/font><\/p>\n<\/blockquote>\n<ul>\n<li>Inhalt in die tempor\u00e4re Datei schreiben<\/li>\n<\/ul>\n<blockquote>\n<p><font face=\"Consolas\">\/\/ Write content to drop into the temporary file       <br \/>File.WriteAllText(tempFile, _txtContent.Text);<\/font><\/p>\n<\/blockquote>\n<ul>\n<li>Tempor\u00e4re Date in ein string-Array verpacken (wird ben\u00f6tigt, da FileDrop ein string array erwartet):<\/li>\n<\/ul>\n<blockquote>\n<p><font face=\"Consolas\">\/\/ Create string-array for FileDrop operations       <br \/>string[] files = new[] { tempFile };<\/font><\/p>\n<\/blockquote>\n<ul>\n<li>DataObject erstellen und mit dem Format der Daten (\u201cFileDrop\u201d) und den Daten instanziieren:<\/li>\n<\/ul>\n<blockquote>\n<p><font face=\"Consolas\">\/\/ Create DataObject for &quot;FileDrop&quot; and add string[] with temporary file       <br \/>DataObject dataObject = new DataObject(&quot;FileDrop&quot;, files);<\/font><\/p>\n<\/blockquote>\n<ul>\n<li>DragDrop initiieren und dataObject \u00fcbergeben:<\/li>\n<\/ul>\n<blockquote>\n<p><font face=\"Consolas\">\/\/ Initiate DragDrop operation       <br \/>DoDragDrop(dataObject, DragDropEffects.All);<\/font><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>M\u00f6chte man per Drag&amp;Drop Dateien aus dem Windows Explorer in die eigene Applikation hereinziehen, so ist das kein Problem: Platzieren eines Controls, auf dem die Datei(en) per Drag&amp;Drop gezogen werden sollen Eigenschaft \u201cAllowDrop\u201d des Controls auf \u201cTrue\u201d setzen: Registrieren auf die Events DragEnter und DragDrop: Im EventHandler f\u00fcr DragEnter pr\u00fcfen, ob es sich im Dateien [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[3,20,21],"tags":[32,38,44,45,47,50,102],"class_list":["post-898","post","type-post","status-publish","format-standard","hentry","category-net","category-visual-studio-net","category-vorlesung","tag-c","tag-dataobject","tag-drag","tag-dragdrop","tag-drop","tag-filedrop","tag-windowsforms"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3Ug90-eu","_links":{"self":[{"href":"https:\/\/abramowitsch.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/898","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abramowitsch.de\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abramowitsch.de\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/abramowitsch.de\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abramowitsch.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=898"}],"version-history":[{"count":0,"href":"https:\/\/abramowitsch.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/898\/revisions"}],"wp:attachment":[{"href":"https:\/\/abramowitsch.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abramowitsch.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abramowitsch.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}