tags:

views:

119

answers:

3

I'm trying to start the del command using System.Diagnostic.Process. Basically I want to delete everything from the C:\ drive that has the filename of *.bat

System.Diagnostics.Process proc = new System.Diagnostics.Process(); string args = string.Empty; args += "*.bat";

proc.StartInfo.FileName = "del"; proc.StartInfo.WorkingDirectory = "C:\"; proc.StartInfo.Arguments = args.TrimEnd(); proc.Start();

However when code is ran an exception is thrown, "the system cannot find specified file." I know there definitely is files in that root folder containing that file extension.

+2  A: 

"del" is not an executable. It's a command run by the command interpreter, cmd.exe. Instead of running "del", run cmd.exe /c "del foo.txt".

Dave Markle
Interesting, I'll bear this in mind for other console commands if I can't find the .Net functionality.
wonea
cmd.exe /c is also useful for those times when you want to run a batch file from the Process class.
Dave Markle
A: 

del is a console command, not an application that can be started through the Process class. Is there a reason you're going at it this way instead of using the managed classes in the System.IO namespace?

Adam Robinson
For this I was barking up the wrong tree, I admit.
wonea
+1  A: 

You do not need to start a del command. You can delete files from C#.

        var files = new DirectoryInfo("C:\\").GetFiles("*.bat");
        foreach (FileInfo fi in files)
        {
            fi.Delete();
        }
Marek
Cracking this works perfectly, and a lot less code!
wonea