Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

c# - Drag and drop virtual files using IStream

I want to enable drag and drop from our windows forms based application to Windows Explorer. The big problem: The files are stored in a database, so I need to use delayed data rendering. There is an article on codeproject.com, but the author is using a H_GLOBAL object which leads to memory problems with files bigger than aprox. 20 MB. I haven't found a working solution for using an IStream Object instead. I think this must be possible to implement, because this isn't an unusual case. (A FTP program needs such a feature too, for example)

Edit: Is it possible to get an event when the user drops the file? So I could for example copy it to temp and the explorer gets it from there? Maybe there is an alternative approach for my problem...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

AFAIK, there is not working article about this for .net. So you should write it by yourself, this is somewhat complicate, because .net DataObject class is limited. I have working example of the opposite task (accepting delayed rendering files from explorer), but it is easier, because I do not needed own IDataObject implementation.

So your task will be:

  1. Find working IDataObject implementation in .net. I recommend you look here (Shell Style Drag and Drop in .NET (WPF and WinForms))
  2. You also need an IStream wrapper for managed stream (it is relatively easy to implement)
  3. Implement delayed rendering using information from MSDN (Shell Clipboard Formats)

This is the starting point, and in general enough information to implement such feature. With bit of patience and several unsuccessful attempts you will do it :)

Update: The following code lacks many necessary methods and functions, but the main logic is here.

// ...

private static IEnumerable<IVirtualItem> GetDataObjectContent(System.Windows.Forms.IDataObject dataObject)
{
  if (dataObject == null)
    return null;

  List<IVirtualItem> Result = new List<IVirtualItem>();

  bool WideDescriptor = dataObject.GetDataPresent(ShlObj.CFSTR_FILEDESCRIPTORW);
  bool AnsiDescriptor = dataObject.GetDataPresent(ShlObj.CFSTR_FILEDESCRIPTORA);

  if (WideDescriptor || AnsiDescriptor)
  {
    IDataObject NativeDataObject = dataObject as IDataObject;
    if (NativeDataObject != null)
    {
      object Data = null;
      if (WideDescriptor)
        Data = dataObject.GetData(ShlObj.CFSTR_FILEDESCRIPTORW);
      else
        if (AnsiDescriptor)
          Data = dataObject.GetData(ShlObj.CFSTR_FILEDESCRIPTORA);

      Stream DataStream = Data as Stream;
      if (DataStream != null)
      {
        Dictionary<string, VirtualClipboardFolder> FolderMap =
          new Dictionary<string, VirtualClipboardFolder>(StringComparer.OrdinalIgnoreCase);

        BinaryReader Reader = new BinaryReader(DataStream);
        int Count = Reader.ReadInt32();
        for (int I = 0; I < Count; I++)
        {
          VirtualClipboardItem ClipboardItem;

          if (WideDescriptor)
          {
            FILEDESCRIPTORW Descriptor = ByteArrayHelper.ReadStructureFromStream<FILEDESCRIPTORW>(DataStream);
            if (((Descriptor.dwFlags & FD.FD_ATTRIBUTES) > 0) && ((Descriptor.dwFileAttributes & FileAttributes.Directory) > 0))
              ClipboardItem = new VirtualClipboardFolder(Descriptor);
            else
              ClipboardItem = new VirtualClipboardFile(Descriptor, NativeDataObject, I);
          }
          else
          {
            FILEDESCRIPTORA Descriptor = ByteArrayHelper.ReadStructureFromStream<FILEDESCRIPTORA>(DataStream);
            if (((Descriptor.dwFlags & FD.FD_ATTRIBUTES) > 0) && ((Descriptor.dwFileAttributes & FileAttributes.Directory) > 0))
              ClipboardItem = new VirtualClipboardFolder(Descriptor);
            else
              ClipboardItem = new VirtualClipboardFile(Descriptor, NativeDataObject, I);
          }

          string ParentFolder = Path.GetDirectoryName(ClipboardItem.FullName);
          if (string.IsNullOrEmpty(ParentFolder))
            Result.Add(ClipboardItem);
          else
          {
            VirtualClipboardFolder Parent = FolderMap[ParentFolder];
            ClipboardItem.Parent = Parent;
            Parent.Content.Add(ClipboardItem);
          }

          VirtualClipboardFolder ClipboardFolder = ClipboardItem as VirtualClipboardFolder;
          if (ClipboardFolder != null)
            FolderMap.Add(PathHelper.ExcludeTrailingDirectorySeparator(ClipboardItem.FullName), ClipboardFolder);
        }
      }
    }
  }

  return Result.Count > 0 ? Result : null;
}

// ...

public VirtualClipboardFile : VirtualClipboardItem, IVirtualFile
{
  // ...

public Stream Open(FileMode mode, FileAccess access, FileShare share, FileOptions options, long startOffset)
{
  if ((mode != FileMode.Open) || (access != FileAccess.Read))
    throw new ArgumentException("Only open file mode and read file access supported.");

  System.Windows.Forms.DataFormats.Format Format = System.Windows.Forms.DataFormats.GetFormat(ShlObj.CFSTR_FILECONTENTS);
  if (Format == null)
    return null;

  FORMATETC FormatEtc = new FORMATETC();
  FormatEtc.cfFormat = (short)Format.Id;
  FormatEtc.dwAspect = DVASPECT.DVASPECT_CONTENT;
  FormatEtc.lindex = FIndex;
  FormatEtc.tymed = TYMED.TYMED_ISTREAM | TYMED.TYMED_HGLOBAL;

  STGMEDIUM Medium;
  FDataObject.GetData(ref FormatEtc, out Medium);

  try
  {
    switch (Medium.tymed)
    {
      case TYMED.TYMED_ISTREAM:
        IStream MediumStream = (IStream)Marshal.GetTypedObjectForIUnknown(Medium.unionmember, typeof(IStream));
        ComStreamWrapper StreamWrapper = new ComStreamWrapper(MediumStream, FileAccess.Read, ComRelease.None);

        // Seek from beginning
        if (startOffset > 0)
          if (StreamWrapper.CanSeek)
            StreamWrapper.Seek(startOffset, SeekOrigin.Begin);
          else
          {
            byte[] Null = new byte[256];
            int Readed = 1;
            while ((startOffset > 0) && (Readed > 0))
            {
              Readed = StreamWrapper.Read(Null, 0, (int)Math.Min(Null.Length, startOffset));
              startOffset -= Readed;
            }
          }

        StreamWrapper.Closed += delegate(object sender, EventArgs e)
        {
          ActiveX.ReleaseStgMedium(ref Medium);
          Marshal.FinalReleaseComObject(MediumStream);
        };

        return StreamWrapper;
      case TYMED.TYMED_HGLOBAL:
        byte[] FileContent;

        IntPtr MediumLock = Windows.GlobalLock(Medium.unionmember);
        try
        {
          long Size = FSize.HasValue ? FSize.Value : Windows.GlobalSize(MediumLock).ToInt64();
          FileContent = new byte[Size];
          Marshal.Copy(MediumLock, FileContent, 0, (int)Size);
        }
        finally
        {
          Windows.GlobalUnlock(Medium.unionmember);
        }
        ActiveX.ReleaseStgMedium(ref Medium);

        Stream ContentStream = new MemoryStream(FileContent, false);
        ContentStream.Seek(startOffset, SeekOrigin.Begin);

        return ContentStream;
      default:
        throw new ApplicationException(string.Format("Unsupported STGMEDIUM.tymed ({0})", Medium.tymed));
    }
  }
  catch
  {
    ActiveX.ReleaseStgMedium(ref Medium);
    throw;
  }
}

// ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...