0

I'm trying to open run a few arguments using cmd.exe from ProcessStartInfo in C# but my folder navigation needs to include double quotes eg. "C:\this is\my\folder site"

as you see the reason for using double quotes is because the folders have space on their name.

this is my code

var ddd = "\"" + projectPath + "\"";
        var strCmdTxt = "/c  cd " + ddd + " && code .";

        System.Diagnostics.Process process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
        {
            WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
            FileName = "cmd.exe",
            Arguments = strCmdTxt, UseShellExecute = true, CreateNoWindow= true
        };
        process.StartInfo = startInfo;
        process.Start();

BUT, what it runs is something like this

cd\ "C:\this is\my\folder site\"

which, just returns me to C drive

The command should be cd "C:\this is\my\folder site"

Victor A Chavez
  • 181
  • 1
  • 19

2 Answers2

0

Looks like what you're trying to achieve is start VS Code in the specified folder. Consider using the working directory of the process you're starting, instead of trying to navigate to that directory and starting VS Code in there. Here is a method to help with that:

private static void StartVSCodeInFolder(string projectPath)
{
    using (System.Diagnostics.Process process = new System.Diagnostics.Process())
    {
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
        {
            WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
            FileName = "C:/Program Files/Microsoft VS Code/Code.exe",
            Arguments = ".",
            UseShellExecute = false,
            CreateNoWindow = true,
            WorkingDirectory = projectPath
        };
        process.StartInfo = startInfo;
        process.Start();
    }
}

Hope this helps.

Artak
  • 2,819
  • 20
  • 31
0

Could you not change the working directory using the Environment class and simply using "code.exe".

It seems like it would be a cleaner approach.

Environment.CurrentDirectory = @"C:\Program Files\Microsoft VS Code";
ForeverZer0
  • 2,379
  • 1
  • 24
  • 32