Quote within String

Status
Not open for further replies.

ShiftySid

Member
Joined
Mar 7, 2005
Messages
15
Programming Experience
3-5
Any ideas why this won't work:

WORKS:

VB.NET:
Shell("shutdown -s -m \\" & txtItemName.Text)

DOESN'T WORK:

VB.NET:
Shell("shutdown -s -m \\" & txtItemName.Text & " " & Chr(34) & "Remote shutdown of this machine has been initiated by IT" & Chr(34))

DOS Syntax for shutdown command:

eg: shutdown -s -m \\XP001 "Being shutdown by IT"

Obviously, I couldn't put quotes within a string as it terminates the string. I am using ASCII chars as per MSDN - the string is perfectly formed as confirmed by outputting the value with MsgBox, but the remote shutdown does not initialise at all.

Without the message tagged on the end, it works.

Any ideas?
 
To put a quote in the string without it closing the string you simply escape the quote char by putting a second quote. IE:

Dim MyString As String = "Here's a quote "", do you like it?"
Messagebox.Show(MyString) ' shows Here's a quote ", do you like it
 
I have achieved the same thing using the ASCII chars, but my question really relates to why won't the command work with the additional parameters in VB yet it works fine from a command line in DOS?
 
I thought you needed -c to add a comment.

VB.NET:
Process.Start(String.Format("shutdown -s -m \\{0} -t 10 -c {1}Your Message{1}", txtItemName.txt, ControlChars.Quote))
 
Hmm trying this but it doesn't work, says it can't find the specified filename.

What do the numbers within the { } brackets mean?
 
Hmm trying this but it doesn't work, says it can't find the specified filename.

What do the numbers within the { } brackets mean?

They're placeholders where your arguments will be entered. In this case {0} is txtItemName.txt and {1} is ControlChars.Quote.

Decided to stop being lazy and slapped this together. Helps if you have a "shutdown -a" to abort while you're testing.

VB.NET:
Dim p As New Process
p.StartInfo.FileName = "cmd.exe"
p.StartInfo.Arguments = String.Format("/c shutdown.exe -s -m \\{0} -t 10 -c {1}{2}{1}", txtMachine.Text, ControlChars.Quote, txtUserMessage.txt)
p.StartInfo.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.System)
p.Start()
 
Status
Not open for further replies.
Back
Top