tags:

views:

1059

answers:

4
System.Diagnostics.Process proc0 = new System.Diagnostics.Process();
proc0.StartInfo.FileName = "cmd";
proc0.StartInfo.WorkingDirectory = Path.Combine(curpath, "snd");
proc0.StartInfo.Arguments = omgwut;

And now for some background...

string curpath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);

omgwut is something like this:

copy /b t.wav + y.wav + p.wav + e.wav + space.wav + i.wav + n.wav + space.wav + s.wav + o.wav + m.wav + e.wav + space.wav + t.wav + e.wav + x.wav + t.wav + space.wav + h.wav + e.wav + r.wav + e.wav + space.wav + a.wav + n.wav + d.wav + space.wav + p.wav + y.wav + r.wav + o.wav + space.wav + w.wav + i.wav + l.wav + l.wav + space.wav + s.wav + h.wav + o.wav + w.wav + space.wav + h.wav + i.wav + s.wav + space.wav + r.wav + e.wav + n.wav + d.wav + i.wav + t.wav + o.wav + n.wav + space.wav output.wav

And nothing happens at all. So obviously something's wrong. I also tried "copy" as the executable, but that doesn't work.

+9  A: 

Try the prefixing your arguments to cmd with /C, effectively saying cmd /C copy /b t.wav ...

According to cmd.exe /? using

/C <command>

Carries out the command specified by string and then terminates

For your code, it might look something like

// .. 
proc0.StartInfo.Arguments = "/C " + omgwut;

Notes:

  • A good way to test whether your command is going to work is to actually try it from a command prompt. If you try to do cmd.exe copy ... you'll see that the copy doesn't occur.
  • There are limits to the length of the arguments you can pass as arguments. From MSDN: "The maximum string length is 2,003 characters in .NET Framework applications and 488 characters in .NET Compact Framework applications."
  • You can bypass the shelling out to command by using the System.IO classes to open the files and manually concatenate them.
Daniel LeCheminant
A: 

Daniels cmd /c idea will work. Keep in mind there is a limit to the length of a command line probably 8k in your case see this for details.

Since you are in a .Net app anyway, File.Copy may be quite a bit easier/cleaner than this approach.

ScottS
He's trying to append multiple files; not sure if you can do that with File.Copy
Daniel LeCheminant
A: 

Try this it might help you.. Its working with my code.

System.Diagnostics.ProcessStartInfo procStartInfo =
    new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);

// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();
// Display the command output.
Console.WriteLine(result);
  }
  catch (Exception objException)
  {
  // Log the exception
  }
vikas latyan
You should add a `throw;` after logging the exception, unless this code is at the top level of the program. That will allow the exception to propagate to the callers.
John Saunders
A: 

Even you can try this.. this is even better.

System.Diagnostics.Process proc = new System.Diagnostics.Process(); 

proc.EnableRaisingEvents=false;
proc.StartInfo.FileName="iexplore";
proc.StartInfo.Arguments=http://www.microsoft.com;

proc.Start();

proc.WaitForExit();

MessageBox.Show("You have just visited www.microsoft.com");
vikas latyan