1
votes

I'm trying to communicate to CMD through my form but I'm running into a "File Path Not Specified" error even though I've given it full information, or have I? I'll try to provide as much information as possible. If you need anything specific, let me know. So how this works is, you hit button1 and it opens command prompt. Then proceeds to load Label3, label1 then label2's text or maybe this process happens all at once(no clue). They're all suppose to work together to build a sequenced code without the user having to touch any part of this process.

Button1's Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Shell("C:\Windows\System32\cmd.exe /k" + Label3.Text + Label1.Text + Label2.text, AppWinStyle.NormalFocus)
End Sub

Then I made form1 load these set of things upon startup.

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Label1.Text = "Wow-64.exe"
    Label2.Text = "-launcherlogin -noautolaunch64bit -launch -uid wow_enus"
    Label3.Text = "cd /d D:\World of Warcraft2"
End Sub

so its loading the text in the labels, on startup, ready to go for the button1's suquence order.

When I click the button I get the file path error as shown here. The file I am trying to mess with is located on my D:\ drive (if this helps, once again no clue). What's weird is, I typed all this code out in CMD and it worked perfectly with no errors but with this, it cannot find the file from what I'm getting. Like I said if I haven't provided enough information just tell me what you need and I'll reply with it.

1
check your spacing. Your string in shell would concatenate to C:\Windows\System32\cmd.exe /kcd /d D:\World of Warcraft2 (note the kcd part)Takarii
@Takarii : Even worse, see my answer.Visual Vincent

1 Answers

0
votes

First of all, never concatenate strings with the + operator, always use the ampersand: &. The + operator can cause problems in the future which you will never experience with the ampersand (See this for a few examples).


Secondly, you seem to be missing a space between /k and Label3.Text, and also between Label1/2/3.Text. This will create a string like:

/kcd /d D:\World of Warcraft2Wow-64.exe-launcherlogin -noautolaunch64bit -launch -uid wow_enus

Instead of the actual:

/k cd /d D:\World of Warcraft2 Wow-64.exe -launcherlogin -noautolaunch64bit -launch -uid wow_enus

By the way you also require an ampersand between your two commands and quotes around them since they include spaces. For instance:

/k cd /d "D:\World of Warcraft2" & Wow-64.exe "-launcherlogin -noautolaunch64bit -launch -uid wow_enus"

Thirdly and finally, I suggest you use the Process.Start() method instead of Shell(). Shell() is outdated and exists only for backwards compability.

The first argument of Process.Start() is the path to the process you want to start and the second is the parameters you want to send to it. Also, instead of specifying the full path of cmd (since it can be different on some computers) just say cmd.exe.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Label1.Text = "Wow-64.exe"
    Label2.Text = "-launcherlogin -noautolaunch64bit -launch -uid wow_enus"
    Label3.Text = "cd /d ""D:\World of Warcraft2""" '<-- Added additional quotes.
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Process.Start("cmd.exe", "/k " & Label3.Text & " " & Label1.Text & " """ & Label2.text & """")
End Sub

Typing two quotes ("") is the fastest way to add a quote to a string in VB.NET, and since it must be surrounded by two other quotes this: """" will create one quote.