将文件从Datagrid拖放到Windows并更改文件名 [英] Drag and Drop File From Datagrid to Windows and Change File Name

查看:61
本文介绍了将文件从Datagrid拖放到Windows并更改文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它显示了与标准windows窗体数据网格控件中显示的记录相关的文档列表。文档存储在文件服务器上,数据网格中的一列具有每个文档的路径。我已经在下面的代码中实现了
,并且我能够成功地从datagrid拖放到桌面或文件夹中并复制文件。我的问题是服务器上的文件是由记录ID(即12345.pdf)命名的。当文件放在桌面上等等时,我会将它们命名为更有意义的
(即Order.pdf,Invoice.pdf等)。如何在拖放操作期间告诉我的程序使用不同的名称保存文件?



Private Sub dgrDocs_Mousedown(sender as Object,e As MouseEventArgs)处理dgrDocs.MouseDown

       试试¥b $ b $
  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;如果e.Button = Windows.Forms.MouseButtons.Left And(Control.ModifierKeys和Keys.Control)<> 0然后

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;昏暗数据As New DataObject

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; data.SetData(" Document",DocumentPath& dgrDocs.Item(dgrDocs.CurrentRowIndex,5))

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Dim files As New System.Collections.Specialized.StringCollection()

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; files.Add(DocumentPath& dgrDocs.Item(dgrDocs.CurrentRowIndex,5))

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; data.SetFileDropList(files)

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; dgrDocs.DoDragDrop(data,DragDropEffects.Copy)

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;结束如果



  &NBSP; &NBSP; &NBSP; C ex ex As Exception

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; MessageBox.Show(ex.Message," Message",MessageBoxButtons.OK,MessageBoxIcon.Error)

  &NBSP; &NBSP; &NBSP;结束尝试



  &NBSP;结束次级

解决方案

马特,


我会折腾这是一个想法:


由于你有完整的文件路径,就在你复制文件之前,创建一个
的实例 IO.FileInfo 因为它会有这里你需要的所有内容。


使用当前的方式复制文件后,请使用。 刷新 方法(将重新读取文件)所以你
可以确认新文件的大小(长度)与。 长度  您刚刚创建的FileInfo实例的。这个
将确保下一步安全:


使用FileInfo实例,使用。 MoveTo 方法本质上是重命名文件。


I have an application that shows a list of documents related to a record displayed in a standard windows forms datagrid control. The documents are stored on a file server and one of the columns in the datagrid has the path to each document. I've implemented the code below and i'm able to successfully drag and drop from the datagrid to the desktop or a folder and the file is copied. My problem is that files on the server are named by record id (i.e 12345.pdf). When the files are placed on the desktop, etc. I'd like them to be named something more meaningful (i.e Order.pdf, Invoice.pdf, etc). How can I tell my program to save the file with a different name during the drag and drop operation?

Private Sub dgrDocs_Mousedown(sender As Object, e As MouseEventArgs) Handles dgrDocs.MouseDown
        Try

            If e.Button = Windows.Forms.MouseButtons.Left And (Control.ModifierKeys And Keys.Control) <> 0 Then
                Dim data As New DataObject
                data.SetData("Document", DocumentPath & dgrDocs.Item(dgrDocs.CurrentRowIndex, 5))
                Dim files As New System.Collections.Specialized.StringCollection()
                files.Add(DocumentPath & dgrDocs.Item(dgrDocs.CurrentRowIndex, 5))
                data.SetFileDropList(files)
                dgrDocs.DoDragDrop(data, DragDropEffects.Copy)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

解决方案

Matt,

I'll toss this out as an idea:

Since you have the full file path, just before you copy the file, create an instance of IO.FileInfo because it will have everything that you'll need here shortly.

Once the file is copied using your current way of doing it, use the .Refresh method (which will reread the file) so you can confirm the size (length) of the new file matches the .Length  of the instance of FileInfo that you just created. This will ensure that the next step is safe to do:

With the instance of FileInfo, use the .MoveTo method which is essentially renaming the file.


这篇关于将文件从Datagrid拖放到Windows并更改文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆