好吧,首先,您在“复制”之后缺少一个空格:
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")