【问题标题】:C# Get files Outlook Attachments single-multiple selected files Win 7, Win 10C# 获取文件 Outlook 附件单选多选文件 Win 7, Win 10
【发布时间】:2019-08-12 20:02:03
【问题描述】:

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

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

public class OutlookDataObject : System.Windows.Forms.IDataObject

Class shown on this post

这个类正在处理 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 !
                //}
            }
        }
}

【问题讨论】:

  • 我想这取决于CopyFile 的实现方式。 Operation 类是什么?
  • 使用try catch获取错误详情,可能是安全问题。
  • 当循环中的值没有改变时,为什么要在循环中设置LastSelectedFolder.GlobalVar?它不应该在第一个 if 块之后设置一次吗?还是我在这里遗漏了什么?
  • 您是否遇到了某种异常? “什么都没有发生”是什么意思?当您在调试器中单步执行代码时,实际上会发生什么?

标签: c# windows copy


【解决方案1】:

我将在这里重播here 的引用。为了让上面的课程在 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));

【讨论】:

    【解决方案2】:

    使用这个: MemoryStream ms = (MemoryStream)dataObject.GetData("FileContents", fileIndex);

    取而代之的是: MemoryStream ms = (MemoryStream)dataObject.GetData("FileContents", true);

    所以它会解析每个文件。

    编辑:

    实际上,除非程序是在 Debug 中编译而不是在 Release 中编译,否则它也不起作用......由于某种原因它只会在 Debug 中起作用

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2012-11-20
      • 1970-01-01
      • 2020-11-25
      • 1970-01-01
      相关资源
      最近更新 更多