2012-11-15 70 views
0

我使用System.IO.File.Copy从serverA的文件复制到ServerB。这工作正常接受文件exsist我得到一个错误“文件已存在”。我尝试使用file.exsist捕获它,但没有任何内容。文件已经存在使用system.io.file.copy

这里是我的代码。

'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) 

我缺少什么?

+0

什么是错误您收到? – Tariqulazam

+0

http://msdn.microsoft.com/en-us/library/system.io.file.copy.aspx – jrummell

+0

对不起错误是在我的主题行“文件已经存在” – Gee

回答

1

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

System.IO.File.Copy(fileName, destName, overwrite); 
+0

如上当我使用说明这个参数我得到另一个错误“访问路径denided”。如果我删除了允许我再次写入路径的文件,则不是这样。 – Gee

+0

可能是运行应用程序池的用户没有此文件夹所需的权限。检查您的iss设置以查看哪个用户运行您的应用程序池并检查它是否作为该文件夹的权限。 – Machinegon

2

如果你只需要修改你这样的复制操作,它应该工作。最后一个参数将覆盖文件。

System.IO.File.Copy(localPath + FileName, remotePath + FileName, True); 
+0

现在我得到了一个不同的错误“访问路径denided”。如果我删除了允许我再次写入路径的文件,则不是这样。如果我带走参数True,则会出现另一个错误“文件已存在”。必须有一种方法来解决它。 – Gee

+0

尝试使用目标文件夹上的“完全控制”权限。这也可能是一些假冒问题。 – Tariqulazam

+0

我的服务器管理员说完全控制已经在这个文件夹中。他说这可能是这台服务器上的HIPS问题。他现在正在检查它。 – Gee

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); 
} 
+0

我做了这一个alreay,文件是有加上我可以看到它,因为它复制,我有资源管理器在远程服务器上打开,因为我正在测试。 – Gee

+0

你能否显示你的代码来检查文件是否存在? – RLG

+0

它正在工作,我正在使用您的代码来检查我的工作。如果不是新的System.IO.FileInfo(remotePath + FileName).Exists然后 File.Copy(localPath + FileName,remotePath + FileName,覆盖) End If – Gee

-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 
相关问题