2013-08-23 66 views
0

我試圖複製local text file這是我的工作目錄到其他remote desktop複製本地文本文件到遠程桌面

這是我試圖做提到的方式here

ExecuteCommand("Copy" & Directory.GetCurrentDirectory & "\Output\Files.txt \\myservername -u username -p password C$\Files.txt")

Public Sub ExecuteCommand(ByVal Command As String) 
     Dim ProcessInfo As ProcessStartInfo 
     Dim Process As Process 
     ProcessInfo = New ProcessStartInfo("cmd.exe", "/K" & Command) 
     ProcessInfo.CreateNoWindow = True 
     ProcessInfo.UseShellExecute = True 
     Process = Process.Start(ProcessInfo) 
End Sub 

我GETT荷蘭國際集團這樣的錯誤:

The filename, directory name or volume label syntax is incorrect

回答

1

嗯,首先,你缺少的 「複製」 後面輸入一個空格:

ExecuteCommand("Copy" & Directory.GetCurrentDirectory & ... 

,將變成(鑑於當前目錄以「C:\ MYDIR」爲例)

cmd.exe /kCopyC:\MYDIR 

缺少空間af ter /k選項cmd.exe不是問題,但看起來很尷尬。我也會在那裏放一個。

其次,"\\myservername -u username -p password C$\Files.txt"看起來錯了。你的例子可能應該是"\\myservername\C$\Files.txt"。用戶名和密碼在這一點和Copy命令(複製過去錯誤?)的上下文中沒有意義。

然後你在你的問題的「ExecuteCommand ...」例子中有一些虛假(?)行包裝。可能是因爲這些問題導致了更多的問題,但這很難說明問題。

ExecuteCommand方法(或使用調試器)中輸出Command變量的值並檢查它是否正常。另外,首先嚐試從命令行執行整個事情以確保它能夠正常工作。

全部放在一起,我會寫這樣的:

ExecuteCommand("Copy " & Directory.GetCurrentDirectory & "\Output\Files.txt \\myservername\C$\Files.txt") 

' ... 

Public Sub ExecuteCommand(ByVal Command As String) 
     Dim ProcessInfo As ProcessStartInfo 
     Dim Process As Process 
     ProcessInfo = New ProcessStartInfo("cmd.exe", "/K " & Command) 
     ProcessInfo.CreateNoWindow = True 
     ProcessInfo.UseShellExecute = True 
     Process = Process.Start(ProcessInfo) 
     ' You might want to wait for the copy operation to actually finish. 
     Process.WaitForExit() 
     ' You might want to check the success of the operation looking at 
     ' Process.ExitCode, which should be 0 when all is good (in this case). 
     Process.Dispose() 
End Sub 

最後,你可以只使用File.Copy代替。無需調用cmd.exe爲:

File.Copy(Directory.GetCurrentDirectory & "\Output\Files.txt", 
    "\\myservername\C$\Files.txt") 
+0

@ Christian.K-首先感謝您的詳細解釋,如果我使用上述語法File.Copy(...)它給了我和錯誤,指出「登錄失敗..Bad用戶名或密碼「,但能夠使用相同的用戶名和密碼打開遠程桌面。 – coder

+0

您需要確保用戶(最終運行'File.Copy'或您的'ExecuteCommand')實際上具有對目標(即\\ myservername \ c $ \')的寫訪問權限。 –

+0

我正在使用file.copy,並且我剛剛檢查了「C $」..它具有完整的讀寫和執行權限。 – coder

相關問題