Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle values with spaces in Process.Start in C#

Tags:

c#

.net

windows

I have a button, and I use Process.Start when clicking it, although I select data from textBox1.Text.

Although this data on textBox1.Text does not come out properly if there are spaces in textBox1.Text

e.g. textBox1.Text = testing_123 works

although textBox1.Text = testing 1 2 3 doesn't work (it will only include "testing")

The code is below:

    private void button19_Click(object sender, EventArgs e)
    {
        Process.Start("test.exe", textBox1.Text);
    }
like image 1000
Michael Avatar asked Jun 16 '11 21:06

Michael


People also ask

How do you pass a string with spaces?

If you want to pass named string arguments containing spaces in between to the python script, you can simply put the text in double quotes.

How can you send an argument to a program that includes a space or spaces?

How do you pass a command line argument with spaces? By enclosing the path (or parts of it) in double quotation marks ( ” ). By adding a caret character ( ^ ) before each space. (This only works in Command Prompt/CMD, and it doesn't seem to work with every command.)

How does process Start work?

Start(String, String) Starts a process resource by specifying the name of an application and a set of command-line arguments, and associates the resource with a new Process component.

What is process c#?

C# Process class provides Start method for launching an exe from code. The Process class is in the System. Diagnostics namespace that has methods to run a .exe file to see any document or a webpage. The Process class provides Start methods for launching another application in the C# Programming.


2 Answers

Simply quote the args like this before passing:

private void button19_Click(object sender, EventArgs e)
{
    Process.Start("test.exe", "\"" + textBox1.Text + "\"");
}
like image 173
Teoman Soygul Avatar answered Nov 02 '22 22:11

Teoman Soygul


Add quotes around your argument string.

like image 21
500 - Internal Server Error Avatar answered Nov 03 '22 00:11

500 - Internal Server Error