0

我正在使用 System.IO.File.Copy 将文件从服务器 A 复制到服务器 B。当文件存在时,这很好接受我收到错误“文件已存在”。我尝试使用 if file.exsist 来捕获它,但什么也没有。

这是我的代码。

'Save files to disk
 FileUpload1.SaveAs(Server.MapPath("../pdf/audits/" & FileName))
 'Local Server
 Dim localPath As String = "\\server01\folder1$\pdf\audits\"
 'Remote Server
 Dim remotePath As String = "\\server02\folder2$\pdf\audits\"
 System.IO.File.Copy(localPath + FileName, remotePath + FileName)

我错过了什么?

4

4 回答 4

2

如果您只是像这样修改您的复制操作,它应该可以工作。最后一个参数将覆盖文件。

System.IO.File.Copy(localPath + FileName, remotePath + FileName, True);
于 2012-11-15T21:12:15.933 回答
1

如果它已经存在,还有第三个参数要覆盖

System.IO.File.Copy(fileName, destName, overwrite);
于 2012-11-15T21:11:11.103 回答
1

如果你有大文件,你不会想每次都覆盖它们。尝试修复检查以查看文件是否存在。像这样的东西(C#):

var localPath = @"C:\";
var remotePath = @"\\server\folder\";
var fileName = "test.txt";

if (!new System.IO.FileInfo(remotePath + fileName).Exists)
{
    System.IO.File.Copy(localPath + fileName, remotePath + fileName);
}
于 2012-11-15T21:26:25.607 回答
-1
     I got it working with help from RLG.  

       'Save files to disk
        FileUpload1.SaveAs(Server.MapPath("../pdf/audits" & FileName))
        'SIGAR Public CMS
        Dim localPath As String = "\\hqdadev01\sigar_cms$\pdf\audits\"
        'SIGAR Dev
        Dim remotePath As String = "\\hqdadev02\sigar_public$\pdf\audits\"

添加了这个来检查。

    If Not New System.IO.FileInfo(remotePath + FileName).Exists Then
        File.Copy(localPath + FileName, remotePath + FileName, overwrite)
    End If
于 2012-11-15T22:25:54.877 回答