将文件从一个路径复制到另一个路径

时间:2014-12-17 04:03:47

标签: c# vb.net winforms file copy

从数据库中获取文件名和文件路径,如下所述:

        msSQL = " select file_name,file_path FROM myTable where file_id='F012'"
        Dim Reader = myCommonFun.GetDataReader(msSQL)
        Dim file_path As String = Nothing
        If Reader.HasRows = True Then
            Reader.Read()
            Dim file_Name as string = Reader.Item("file_name").ToString
            Dim file_path = Reader.Item("file_path").ToString
        End If

现在我需要的是我想在查询返回的路径中获取文件并将同一文件复制到另一个位置。因为我使用的代码段为:

Dim destinationFile as string = Server.MapPath("../uploaded/" & file_Name)
File.Copy(file_path , destinationFile, True)

但它不会像预期的那样结果,做错了什么?我怎样才能达到目标?

更新

  • in File.Copy(file_path , destinationFile, True)
  • file_path:D:\new\data\me.doc
  • destinationFile:D:\web\mypro\uploaded\me.doc

1 个答案:

答案 0 :(得分:2)

您应该将file_path与file_Name结合使用扩展名 请尝试下面的代码

Dim destinationFile as string = Server.MapPath("../uploaded/" & file_Name)
File.Copy(file_path , destinationFile, True)

更改为

Dim destinationFile as string = Server.MapPath("../uploaded/" & file_Name)
file_path =file_path &"\\"& file_Name
File.Copy(file_path , destinationFile, True)