James
James

Reputation: 79

C# - Problem with Process.Start statement

I am having problems when executing an event in C# using Process.Start. The statement below only outputs half of the command:

private void AddTask_Click(object sender, EventArgs e)
{
    Process.Start("schtasks.exe", string.Format(@"/Create /SC DAILY /TN", "\"" + textBox1.Text + "\"", string.Format(@"/TR C:\Program Files\test\scanner.exe C:\", "\"" + textBox1.Text + "\"")));
}

For some reason it cuts of at "/TN" e.g.

"C:\Windows\System32\schtasks.exe" /Create /SC DAILY /TN

Upvotes: 0

Views: 841

Answers (2)

Henk Holterman
Henk Holterman

Reputation: 273621

For some reason it cuts of at "/TN"

Correct. In

 string.Format(@"/Create /SC DAILY /TN", "other strings");

The first string is seen as the format string, the rest are arguments, unused in this case.
Without {0} place holders you don't need String.Format(), simply use

 Process.Start("schtasks.exe", @"/Create /SC DAILY /TN" + "\"" + ...

That doesn't exclude the possibility of a syntax error in your commandline arguments.

Change it to :

string args =  @"/Create /SC DAILY /TN" + "\"" + ...
Process.Start("schtasks.exe", args);

And then you can inspect args in the debugger and maybe post here.

Upvotes: 1

Brian Ball
Brian Ball

Reputation: 12606

I'm surprised an exception isn't being thrown. You format string is @"/Create /SC Daily /TN". It doesn't have any place holders (i.e. {0}), so it has no where to put the values passed in for all the other parameters of string.Format().

If you can post an example of what the output should look like (it's hard to tell from your code example), then either I, or someone else, should be able to give you the proper string.Format() you need to use.

Upvotes: 0

Related Questions