1

I'm creating an application in Visual Studio. When the user clicks a button, I want the follow CMD command to be executed:

xcopy /s/y "C:\myfile.txt" "D:\"

I've tried this with Process.Start() but it won't work. The button code is:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Process.Start("CMD", "/C xcopy /s/y "C:\myfile.txt" "D:\"")
End Sub

Does anyone know how I can make this work? I suspect that the problem is caused by the /s/y parameters or quotes in the CMD command.

YowE3K
  • 23,852
  • 7
  • 26
  • 40
elton73
  • 463
  • 1
  • 6
  • 16

1 Answers1

3

Your code won't compile: you need to escape the double-quotes in the string. In VB.NET you escape quotes using double-double quotes:

Process.Start( "CMD", "/C xcopy /s/y ""C:\myfile.txt"" ""D:\""" )
Dai
  • 141,631
  • 28
  • 261
  • 374
  • This works fine, thanks! However, the command prompt window stays open after clicking the button. Do you know how I can close that automatically after executing the xcopy, something like the exit command or even better, don't show the CMD screen at all to the user? – elton73 Apr 14 '17 at 21:06
  • @elton73: `xcopy /?` – Sam Axe Apr 14 '17 at 23:02
  • @elton73 : See [**this**](http://stackoverflow.com/a/12477832/3740093), and also append `& exit` before the last quote. – Visual Vincent Apr 14 '17 at 23:11
  • @Visual Vincent: as a newbie to VB I'm not sure how to apply this answer to my code. This won't work: Process.Start( "CMD", "/C xcopy /s/y ""C:\myfile.txt"" ""D:\"" & exit" ). – elton73 Apr 15 '17 at 09:21
  • @elton73 : That should at least close it. The answer I linked to you can just copy-paste and write your arguments in place of `CmdStr`: `startInfo.Arguments = "/C xcopy /s/y ""C:\myfile.txt"" ""D:\"" & exit"`. – Visual Vincent Apr 15 '17 at 09:54
  • @Visual Vincent: Yes! Now I got it working by applying this solution. Thanks a lot! :) – elton73 Apr 15 '17 at 18:42
  • @elton73 : Glad I could help! Good luck! – Visual Vincent Apr 15 '17 at 18:46