当前位置: 首页 > 编程语言 > C#

如何使用filegroupdescriptor拖拽一个文件到explorerc#share

时间:2023-04-11 03:29:19 C#

如何使用filegroupdescriptor拖拽一个文件到explorerc#当拖放开始时,我需要按需准备文件并将其保存到内存流中。您能否提供一个使用FileGroupDescriptor数据结构的示例?谢谢。Andrea您可以在此处找到有关如何使用C#将虚拟文件传输到Windows资源管理器的示例;这里还有一些关于这个主题的好信息:C#中的Outlook拖放简而言之,您需要做的是使用FILEDESCRIPTOR初始化DataObject(您可以在pinvoke.net上找到它的声明详细信息)结构,以便传输文件及其内容。下面是一个关于如何将1个文件从winformsListBox传输到资源管理器的示例。鼠标按下列表框的事件处理程序:privatevoidlistBox1_MouseDown(objectsender,MouseEventArgse){DataObjectdataObject=newDataObject();DragFileInfofilesInfo=newDragFileInfo("d:\test.txt");使用(MemoryStreaminfoStream=GetFileDescriptor(filesInfo),contentStream=GetFileContents(filesInfo)){dataObject.SetData(CFSTR_FILEDESCRIPTORW,infoStream);dataObject.SetData(CFSTR_FILECONTENTS,contentStream);dataObject.SetData(CFSTR_PERFORMEDROPEFFECT,null);DoDragDrop(dataObject,DragDropEffects.All);}}初始数据对象所需要的代码:[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]structFILEDESCRIPTOR{publicUInt32dwFlags;公共Guidclid;publicSystem.Drawing.Sizesizel;publicSystem.Drawing.Pointpointl;公共UInt32dwFileAttributes;publicSystem.Runtime.InteropServices.ComTypes.FILETIMEftCreationTime;公共System.Runtime.InteropServices.ComTypes.FILETIMEftLastAccessTime;公共System.Runtime.InteropServices.ComTypes.FILETIMEftLastWr项目时间;公共UInt32nFileSizeHigh;公共UInt32nFileSizeLow;[MarshalAs(UnmanagedType.ByValTStr,SizeConst=260)]publicStringcFileName;}publicconststringCFSTR_PREFERREDROPEFFECT="PreferredDropEffect";publicconststringCFSTR_PERFORMEDDROPEFFECT="PerformedDropEffect";publicconststringCFSTR_FILEDESCRIPTORW="FileGroupDescriptorW";publicconststringCFSTR_FILECONTENTS="FileContents";publicconstInt32FD_WRITESTIME=0x00000020;publicconstInt32FD_FILESIZE=0x00000040;publicconstInt32FD_PROGRESSUI=0x00004000;publicstructDragFileInfo{publicstringFileName;公共字符串源文件名;公共日期时间写入时间;公共Int64文件大小;publicDragFileInfo(stringfileName){FileName=Path.GetFileName(fileName);源文件名=文件名;WriteTime=DateTime.Now;FileSize=(newFileInfo(fileName)).L??ength;}}privateMemoryStreamGetFileDescriptor(DragFileInfofileInfo){MemoryStreamstream=newMemoryStream();stream.Write(BitConverter.GetBytes(1),0,sizeof(UInt32));FILEDESCRIPTORfileDescriptor=newFILEDESCRIPTOR();fileDescriptor.cFileName=fileInfo.FileName;Int64fileWriteTimeUtc=fileInfo.WriteTime.ToFileTimeUtc();fileDescriptor.ftLastWriteTime.dwHighDateTime=(Int32)(fileWriteTimeUtc>>32);fileDescriptor.ftLastWriteTime.dwLowDateTime=(Int32)(fileWriteTimeUtc&0xFFFFFFFF);fileDescriptor.nFileSizeHigh=(UInt32)(fileInfo.FileSize>>32);fileDescriptor.nFileSizeLow=(UInt32)(fileInfo.FileSize&0xFFFFFFFF);文件描述符.dwFlags=FD_WRITESTIME|文件大小|FD_PROGRESSUI;Int32fileDescriptorSize=Marshal.SizeOf(fileDescriptor);IntPtrfileDescriptorPointer=Marshal.AllocHGlobal(fileDescriptorSize);Byte[]fileDescriptorByteArray=newByte[fileDescriptorSize];尝试{Marshal.StructureToPtr(fileDescriptor,fileDescriptorPointer,true);Marshal.Copy(fileDescriptorPointer,fileDescriptorByteArray,0,fileDescriptorSize);}最后{Marshal.FreeHGlobal(fileDescriptorPointer);}stream.Write(fileDescriptorByteArray,0,fileDescriptorByteArray.Length);返回流;}privateMemoryStreamGetFileContents(DragFileInfofileInfo){MemoryStream(new)readerstream=reader=newBinaryReader(File.OpenRead(fileInfo.SourceFileName))){Byte[]buffer=newByte[fileInfo.FileSize];reader.Read(buffer,0,(Int32)fileInfo.FileSize);如果(buffer.Length==0)buffer=newByte[1];stream.Write(buffer,0,buffer.Length);}返回流;希望这能让您了解如何进行,问候将文件拖到资源管理器c#以共享所有内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击维权联系管理员删除,如有转载请注明出处: