首页 > 解决方案 > C# 获取文件 Outlook 附件单选多选文件 Win 7, Win 10

问题描述

我重新编辑了我的问题,因为问题出在其他地方。

我有这段代码可以以特定的获胜形式从 Outlook(单个或多个)中删除文件。在 Windows 7 站上,复制已制作,但在 Windows 10 上无法从类中获取文件名列表。

public class OutlookDataObject : System.Windows.Forms.IDataObject

这篇文章中显示的课程

这个类正在为win 7工作,但在windwos 10上没有文件名返回。这个巨大的类超出了我的理解。有一种简单的方法可以从 Outlook 中获取选定的附件以准备删除?

 private void btn_Home_DragDrop(object sender, DragEventArgs e)
    {
        bool debug = true;
        if (debug) { txt_FileInfo.AppendText("Entering drop method " + Environment.NewLine); }

        folderBrowserDialog1.SelectedPath = LastSelectedFolder.GlobalVar;

        if (debug)
        { txt_FileInfo.AppendText("Get last path " + Environment.NewLine); }

        folderBrowserDialog1.Description = "Drop the files";

        if (debug)
        { txt_FileInfo.AppendText("Show folder dialog " + Environment.NewLine); }

        if (folderBrowserDialog1.ShowDialog() != DialogResult.OK)
        {
            return;
        }


        LastSelectedFolder.GlobalVar = folderBrowserDialog1.SelectedPath.ToString();

        if (debug)
        { txt_FileInfo.AppendText("Path is selected " + LastSelectedFolder.GlobalVar + Environment.NewLine); }

        string[] fileNames = null;

        if (debug)
        { txt_FileInfo.AppendText("Prepare to transfer " + Environment.NewLine); }

        if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
        {

            if (debug)
            { txt_FileInfo.AppendText("DataFormats.FileDrop " + Environment.NewLine); }

            fileNames = (string[])e.Data.GetData(DataFormats.FileDrop);
            foreach (string fileName in fileNames)
            {
                // do what you are going to do with each filename
                string destinationFile = Path.Combine(folderBrowserDialog1.SelectedPath, Path.GetFileName(fileName));

                if (debug)
                { txt_FileInfo.AppendText("Destination File " + destinationFile + Environment.NewLine); }

                if (Operation.CopyFile(fileName, destinationFile, ci))
                {
                    txt_FileInfo.AppendText("File have been copied to " + destinationFile + Environment.NewLine);
                }
            }
        }
        else if (e.Data.GetDataPresent("FileGroupDescriptor"))
        {

            if (debug)
            { txt_FileInfo.AppendText("FileGroupDescriptor " + Environment.NewLine); }

            OutlookDataObject dataObject = new OutlookDataObject(e.Data);
            string[] filenames = (string[])dataObject.GetData("FileGroupDescriptor");

            for (int fileIndex = 0; fileIndex < filenames.Length; fileIndex++)
            {

                if (debug)
                { txt_FileInfo.AppendText("Files in attachement " + filenames[fileIndex] + Environment.NewLine); }

                string path = Path.GetTempPath();
                // put the zip file into the temp directory
                string theFile = path + filenames[fileIndex].ToString();
                // create the full-path name

                if (debug)
                { txt_FileInfo.AppendText("Get temp Path " + theFile + Environment.NewLine); }

                //
                // Second step:  we have the file name.
                // Now we need to get the actual raw
                // data for the attached file and copy it to disk so we work on it.
                //

                // get the actual raw file into memory
                MemoryStream ms = (MemoryStream)e.Data.GetData(
                    "FileContents", true);
                // allocate enough bytes to hold the raw data
                byte[] fileBytes = new byte[ms.Length];
                // set starting position at first byte and read in the raw data
                ms.Position = 0;
                ms.Read(fileBytes, 0, (int)ms.Length);
                // create a file and save the raw zip file to it
                FileStream fs = new FileStream(theFile, FileMode.Create);
                fs.Write(fileBytes, 0, (int)fileBytes.Length);

                fs.Close();  // close the file

                FileInfo tempFile = new FileInfo(theFile);

                // always good to make sure we actually created the file
                if (tempFile.Exists == true)
                {
                    // for now, just delete what we created

                    string fileName = tempFile.FullName;

                    string destinationFile = Path.Combine(folderBrowserDialog1.SelectedPath, Path.GetFileName(fileName));

                    if (debug)
                    { txt_FileInfo.AppendText("destinationFile " + destinationFile + Environment.NewLine); }


                    if (debug)
                    { txt_FileInfo.AppendText("Prepare to copy " + destinationFile + Environment.NewLine); }

                    if (Operation.CopyFile(fileName, destinationFile, ci))
                    {
                        txt_FileInfo.AppendText("File have been copied to " + destinationFile + Environment.NewLine);
                    }
                    else
                    {
                        if (debug)
                        { txt_FileInfo.AppendText("Copy failed " + " Source " + fileName + " Destination " + destinationFile + Environment.NewLine); }
                    }

                    tempFile.Delete();

                    if (debug)
                    { txt_FileInfo.AppendText("Delete temp file " + tempFile + Environment.NewLine); }

                }

                else
                { Trace.WriteLine("File was not created!"); }

                //    catch (Exception ex)
                //{
                //    Trace.WriteLine("Error in DragDrop function: " + ex.Message);

                //    // don't use MessageBox here - Outlook or Explorer is waiting !
                //}
            }
        }
}

标签: c#windowscopy

解决方案


我将在这里重播这里的引用。让上面的课程在 win 8 + 几行要更改(从 int 到 long)

从:

IntPtr fileDescriptorPointer = (IntPtr)((int)fileGroupDescriptorWPointer + Marshal.SizeOf(fileGroupDescriptor.cItems));

IntPtr fileDescriptorPointer = (IntPtr)((long)fileGroupDescriptorWPointer + Marshal.SizeOf(fileGroupDescriptor.cItems));

从:

fileDescriptorPointer = (IntPtr)((int)fileDescriptorPointer + Marshal.SizeOf(fileDescriptor));

fileDescriptorPointer = (IntPtr)((long)fileDescriptorPointer + Marshal.SizeOf(fileDescriptor));

推荐阅读