[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 742
  • Last Modified:

pass argument to batch file with space in the argument

I have a console application written in C# that starts a process and I need to pass an argument to it that has a space in the name. This example shows how I can have the batch file start an application that I set as the parameter. I used it to launch notepad, but if I put a space between note and pad it no longer works.

                try
                {
                    string targetDir;
                    targetDir =
                      string.Format(@"W:\");
                    p = new Process();
                    p.StartInfo.WorkingDirectory = targetDir;
                    p.StartInfo.FileName = "test.bat";

                    p.StartInfo.Arguments =
                      string.Format("note pad");
                    p.StartInfo.CreateNoWindow = false;
                    p.Start();
                    p.WaitForExit();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception Occurred :{0},{1}",
                                ex.Message, ex.StackTrace.ToString());
                }

Open in new window


How can I pass that argument with the space in it?
0
wiggy353
Asked:
wiggy353
  • 4
  • 3
1 Solution
 
Steve KnightIT ConsultancyCommented:
You need double quotes around the argument on the command line.  As to how to escape the " characters in C#,not my language.  
May involve referring to it as character 34, doubling up "" or using \" etc.
0
 
Steve KnightIT ConsultancyCommented:
0
 
Steve KnightIT ConsultancyCommented:
So I presume that is

string.Format("\"note pad\"");

or
string.Format("""note pad""");

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
wiggy353Author Commented:
Tried both of those. Still just sees the argument as note.
0
 
wiggy353Author Commented:
Actually, when I do it with the "\"note pag\"", it opens a new console window titled "note pad", but doesn't launch the note pad.exe application.
0
 
wiggy353Author Commented:
which is actually what it does if I hard code note pad into the batch file... hmm.. so it is passing the argument correctly, I must need to do something with the batch file.
0
 
Steve KnightIT ConsultancyCommented:
I presume you are using something else to note pad.exe as of course it's real name is notepad.exe ?

Ahh so if that is doing the same as start then?...
If so the first command in "" is the title.  The second is the progr. to run, so
try
 "\"Some title\" note pad.exe"

Anyway I see this already in c# zone so should leave it to someone who knows the language...
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now