2012-11-15 72 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 
相關問題