嗯,首先,你缺少的 “复制” 后面输入一个空格:
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")
@ Christian.K-首先感谢您的详细解释,如果我使用上述语法File.Copy(...)它给了我和错误,指出“登录失败..Bad用户名或密码“,但能够使用相同的用户名和密码打开远程桌面。 – coder
您需要确保用户(最终运行'File.Copy'或您的'ExecuteCommand')实际上具有对目标(即\\ myservername \ c $ \')的写访问权限。 –
我正在使用file.copy,并且我刚刚检查了“C $”..它具有完整的读写和执行权限。 – coder