user979033
user979033

Reputation: 6430

How to pass CMD argument with spaces?

I want to run simple CMD command and my argument contains spaces (" ").

In this case this is not working since its recognized as command with several arguments...

This is what I have tried (same results):

string arg = "c:\my path\ this is test\file.doc";

string.Format("\"{0}\"", arg)

Edit:

public void Invoke(string fileName, string arg)
{
    ProcessStartInfo processStartInfo = new ProcessStartInfo();
    processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    processStartInfo.FileName = fileName;
    if (arg != "")
        processStartInfo.Arguments = arg;
    processStartInfo.RedirectStandardOutput = true;
    processStartInfo.RedirectStandardError = true;
    processStartInfo.RedirectStandardInput = true;
    processStartInfo.UseShellExecute = false;
    processStartInfo.CreateNoWindow = true;

    Process process = new Process();
    process.StartInfo = processStartInfo;
    process.Exited += Process_Exited;
    process.EnableRaisingEvents = true;
    process.Start();
    process.BeginOutputReadLine();
    process.BeginErrorReadLine();
}

Usage:

string arg = "c:\my path\this is my string.ps1";   
Invoke(@"C:\windows\system32\windowspowershell\v1.0\powershell.exe", string.Format("\"{0}\"", arg)); 

Upvotes: 0

Views: 3813

Answers (2)

sifa vahora
sifa vahora

Reputation: 117

You can try this :

string arg = @"c:\\"my path\"\\"this is my string.ps1\"";   
Invoke(@"C:\windows\system32\windowspowershell\v1.0\powershell.exe", 
string.Format("\"{0}\"", arg));

or string arg = @"c:/\"my path\"/\"this is my string.ps1\"";

Upvotes: 1

Theo
Theo

Reputation: 885

Within your application join all the arguments together to get a single space separated argument like this:

var joinedArgs = string.Join(" ", args);

Then pass the joined args to your function.

Upvotes: 0

Related Questions