猿问

从另一个应用程序拖放 C# Windows 窗体应用程序

我试图从另一个 Windows 应用程序(EM 客户端、thunderbird 或 Outlook)拖到我的表单上。当电子邮件从其他应用程序拖到 windows explore 时,它会作为一个文件放置。如果用户拖动到我的应用程序上,我希望将文件内容作为文件流获取。


我能够在 UWP 应用程序中使用它,但我需要让它在 Windows 窗体应用程序中运行,以便它可以在 Windows 7 中运行。


我发现了很多相反的例子(从应用程序拖到窗口)。


使这如此烦人的事情是在 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文件


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);



                }

            }

        }

    }


}


aluckdog
浏览 162回答 1
1回答

德玛西亚99

因此,在向专业人士寻求帮助后,我有了一个工作示例。诀窍是让“FileDescriptorW”在自定义 ComObject 类中工作。您将在从 Outlook 拖动示例中找到此类的一个版本,但从 EM 客户端拖动时它不起作用,但确实如此。然后你可以像这样使用它:&nbsp; &nbsp; &nbsp; &nbsp; MyDataObject obj = new MyDataObject(e.Data);&nbsp; &nbsp; &nbsp; &nbsp; string[] fileNames = { };&nbsp; &nbsp; &nbsp; &nbsp; //ThunderBird Does a FileDrop&nbsp; &nbsp; &nbsp; &nbsp; if (obj.GetDataPresent(DataFormats.FileDrop, true))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string[] tempFileNames = (string[])obj.GetData(DataFormats.FileDrop);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<string> tempFileNameList = new List<string>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach(string f in tempFileNames)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempFileNameList.Add(Path.GetFileName(f));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fileNames = tempFileNameList.ToArray();&nbsp; &nbsp; &nbsp; &nbsp; } else if (fileNames.Length == 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //EM Client uses "FileGroupDescriptorW"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fileNames = (string[])obj.GetData("FileGroupDescriptorW");&nbsp; &nbsp; &nbsp; &nbsp; }else if (fileNames.Length == 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Outlook Uses "FileGroupDescriptor"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fileNames = (string[])obj.GetData("FileGroupDescriptor");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; int index = 0;&nbsp; &nbsp; &nbsp; &nbsp; foreach (string f in fileNames)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File.WriteAllBytes("C:\\FilePath\\"+f, obj.GetData("FileContents", index).ToArray());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;
随时随地看视频慕课网APP
我要回答