Search code examples
c#command-linesystem.diagnosticsstring-literals

String being misinterpreted or the issue is more intricate


I have a small program which runs a Command line through cmd.exe, purely for comfort.

It's basically designed to give me the option in a single program to wake up another PC (or directly connect to it using TightVNC to put it in stand-by mode).

The issue I'm having is as follows:

This piece of code here:

string strCmdText = @"cd /D C:\ && cd Program Files\TightVNC && _
 tvnviewer.exe 192.168.0.233 -password=******";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);

For some reason this code isn't working for me. It's not giving me any output in the command terminal, nor does it open the TightVNC process, while when I simply copy/paste this into my command line, it works just fine, instantly opening the connection.

I thought: "Maybe it's the eventual output for the strCmdText variable having double backslashes, but as the same method worked before in this code:

string path = @"C:\Users\Yorrick\Desktop\wakemeonlan\WakeMeOnLan.exe";
string commandText = path + @" /wakeup 192.168.0.233";
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = commandText;
process.StartInfo = startInfo;

There, the commandText variable has the double backslashes aswell (\\).

Anyone that could help me find a solution for this? It's not that I REALLY need this program, but I'd like having it as it'd save me a bit of time (I like having my Desktop clutter-free, thus I try to use as few shortcuts on there as I possibly can)

Thanks, Yorrick

EDIT: Appearantly adding the "/C" to the start of my command, it does work.

(string strCmdText = @"/C cd /D C:\ && cd Program Files\TightVNC && _ tvnviewer.exe 192.168.0.233 -password=******";)

I thought the usage of /C was simply for closing the command terminal after the command was executed & didn't have anything to do with the command actually being executed or not?

Anyone who can clarify why this suddenly works?


Solution

  • To answer your edit, the /C flag does not mean close — it means execute the following command. So not including the /C flag will not run your command.