![](/img/trans.png)
[英]drag and drop in datagridview using c# windows form application
[英]Drag And Drop C# Windows Form Application From another Application
我正在嘗試從另一個Windows應用程序(EM Client,Thunderbird或Outlook)拖到我的窗體上。 將電子郵件從其他應用程序拖到Windows瀏覽時,它將作為文件刪除。 如果用戶拖動到我的應用程序上,我希望將文件內容作為文件流獲取。
我可以通過UWP應用程序來使用它,但是我需要在Windows Form應用程序中使用它,這樣它才能在Windows 7中運行。
我發現了很多其他方法的示例(從App拖到Windows)。
使它如此令人討厭的是,在UWP應用中很容易。 這是我在UWP應用程序中執行的操作,其結果是將新文件保存在漫游文件夾中,名稱為“ email.eml”:
XAML
<Grid AllowDrop="True" DragOver="Grid_DragOver" Drop="Grid_Drop"
Background="LightBlue" Margin="10,10,10,353">
<TextBlock>Drop anywhere in the blue area</TextBlock>
</Grid>
XAML.CS
namespace App1
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void Grid_DragOver(object sender, DragEventArgs e)
{
e.AcceptedOperation = DataPackageOperation.Copy;
}
private async void Grid_Drop(object sender, DragEventArgs e)
{
if (e.DataView.Contains(StandardDataFormats.StorageItems))
{
var items = await e.DataView.GetStorageItemsAsync();
if (items.Count > 0)
{
var storageFile = items[0] as StorageFile;
var reader = (await storageFile.OpenAsync(FileAccessMode.Read));
IBuffer result = new byte[reader.Size].AsBuffer();
var test = await reader.ReadAsync(result, result.Length,Windows.Storage.Streams.InputStreamOptions.None);
Windows.Storage.StorageFolder storageFolder =
Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile sampleFile = await storageFolder.CreateFileAsync("email.eml",Windows.Storage.CreationCollisionOption.ReplaceExisting);
await Windows.Storage.FileIO.WriteBufferAsync(sampleFile, test);
}
}
}
}
}
我已經閱讀了此答案中列出的每篇文章,以及更多內容: 嘗試從chrome實現拖放gmail附件
基本上,無論我如何進行攻擊,最終我都會得到以下3個結果之一:
這是違反安全性的代碼:
MemoryStream ClipboardMemoryStream = new MemoryStream();
BinaryFormatter bft = new BinaryFormatter();
bft.Serialize(ClipboardMemoryStream, e.Data.GetData("FileGroupDescriptorW", false));
byte[] byteArray = ClipboardMemoryStream.ToArray();
我的猜測是,我需要實現e.Data.GetData(“ FileGroupDesciptorW”)返回一個IStorage類,並且我需要實現該類,但是我不知道該怎么做,而且我不確定案子
e.Data.GetType顯示它的marshalbyrefobject,我嘗試手動進行Remoting,但是我陷入了沒有開放頻道的困境。
https://docs.microsoft.com/zh-CN/windows/desktop/api/objidl/nn-objidl-istorage https://docs.microsoft.com/zh-CN/windows/desktop/shell/datascenarios#dragging -and驚嘆殼對象-異步
因此,在尋求專業人士的幫助后,我有了一個可行的例子。 訣竅是讓“ FileDescriptorW”在自定義ComObject類中工作。 您可以在“從Outlook拖動” 示例中找到該類的版本,但是從EM Client拖動時它不起作用。
代碼如下: 代碼太大,無法發布
然后,您可以像這樣使用它:
MyDataObject obj = new MyDataObject(e.Data);
string[] fileNames = { };
//ThunderBird Does a FileDrop
if (obj.GetDataPresent(DataFormats.FileDrop, true))
{
string[] tempFileNames = (string[])obj.GetData(DataFormats.FileDrop);
List<string> tempFileNameList = new List<string>();
foreach(string f in tempFileNames)
{
tempFileNameList.Add(Path.GetFileName(f));
}
fileNames = tempFileNameList.ToArray();
} else if (fileNames.Length == 0)
{
//EM Client uses "FileGroupDescriptorW"
fileNames = (string[])obj.GetData("FileGroupDescriptorW");
}else if (fileNames.Length == 0)
{
//Outlook Uses "FileGroupDescriptor"
fileNames = (string[])obj.GetData("FileGroupDescriptor");
}
int index = 0;
foreach (string f in fileNames)
{
File.WriteAllBytes("C:\\FilePath\\"+f, obj.GetData("FileContents", index).ToArray());
index++;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.