代码之家  ›  专栏  ›  技术社区  ›  Prince Ashitaka

在WPF中拖放文件传输。如何获取窗口中已删除内容的文件名

  •  4
  • Prince Ashitaka  · 技术社区  · 15 年前

    我正在创建本地文件传输应用程序。我希望用户像skype或其他信使一样,将项目拖放到文件传输应用程序中以启动文件传输。

    放下物品时。已触发丢弃事件。但是,我不知道在哪里可以得到物品的详细信息,如位置、大小等,例如,如果我丢了一张图片。我想看看上面提到的细节。

    AllowDrop &订阅到(amp;S) Drop 事件。[如果有帮助]

    1 回复  |  直到 15 年前
        1
  •  5
  •   vortexwolf    15 年前

    你的意思是文件的大小还是图像的像素大小? 无论如何,请使用以下代码:

    private void Window_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, true))
            {
                string[] droppedFilePaths = e.Data.GetData(DataFormats.FileDrop, true) as string[];
                foreach (var path in droppedFilePaths)
                {          
                    string location = null;
                    int pxWidth = 0, pxHeight = 0;
    
                    FileInfo fi = new FileInfo(path);
                    //fi.Length  //File size
                    //fi.DirectoryName //Directory
                    using (var fs = fi.OpenRead())
                    {
                        try
                        {
                            var bmpFrame = BitmapFrame.Create(fs);
                            var m = bmpFrame.Metadata as BitmapMetadata;
                            if (m != null)
                                location = m.Location;
                            pxWidth = bmpFrame.PixelWidth;
                            pxHeight = bmpFrame.PixelHeight;
                        }
                        catch
                        {
                            //File isn't image
                        }
                    }
    
                    this.fileList.Items.Add(string.Format("({0}x{1}), location: {2}", pxWidth, pxHeight, location));
                }
            }
        }