将本地文本文件复制到远程桌面

时间:2013-08-23 09:18:54

标签: .net windows vb.net

我正在尝试将我工作目录中的 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

我收到了这个错误:

The filename, directory name or volume label syntax is incorrect

1 个答案:

答案 0 :(得分:1)

嗯,首先,你在“复制”之后错过了一个空格:

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

将变为(假设当前目录为“C:\ MYDIR”为例)

cmd.exe /kCopyC:\MYDIR

/k cmd.exe选项之后缺少空格不是问题,但看起来很尴尬。我也把它放在那里。

其次,"\\myservername -u username -p password C$\Files.txt"看起来错了。根据你的例子,这应该是"\\myservername\C$\Files.txt"。此时和Copy命令(复制过去错误?)的上下文中,用户名和密码没有意义。

然后你在问题的“ExecuteCommand ...”示例中有一些伪造的(?)行包装。可能是那些引起了更多问题,但这很难说清楚。

Command方法中输出ExecuteCommand变量的值(或使用调试器)并检查它是否合理。另外,首先尝试从命令行执行整个操作以确保它正常工作。

总而言之,我会这样写:

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