0

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

1
  • 1
    You might also want to check out the (good) answers to your other question that show you how to do it (in a better way) using ProcessStartInfo Commented Jun 19, 2011 at 16:05

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Thanks, glad someone gets the situation :)
0

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.

1 Comment

You don't need to use string.Format() you can use straight string concatenation, but string.Format() generally makes code much more readable. However, if you are OK with hard-coding paths, then try: @"/Create /SC DAILY /TN Scan /TR ""C:\Program Files\test\scanner.exe 'C:\"""

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.