Michael
Michael

Reputation: 121

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

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);
    }

Upvotes: 5

Views: 742

Answers (3)

Cos Callis
Cos Callis

Reputation: 5084

If you just want to get rid of the spaces:

TextBox1.Text.Replace(" ",string.Empty)

Or if you want to substitute another character (underscore) then try:

TextBox1.Text.Replace(" ","_")

If you want to include the space then @Teoman has your answer...

It depends on what you mean by "handle".

Upvotes: 0

Teoman Soygul
Teoman Soygul

Reputation: 25742

Simply quote the args like this before passing:

private void button19_Click(object sender, EventArgs e)
{
    Process.Start("test.exe", "\"" + textBox1.Text + "\"");
}

Upvotes: 4

Add quotes around your argument string.

Upvotes: 2

Related Questions