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.

1 Answer 1

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:\""" )
Sign up to request clarification or add additional context in comments.

7 Comments

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: xcopy /?
@elton73 : See this, and also append & exit before the last quote.
@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 : 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".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.