wonea
wonea

Reputation: 4969

System.Diagnostics.Process - Del Command

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.

Upvotes: 2

Views: 2543

Answers (3)

Marek
Marek

Reputation: 10402

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

Upvotes: 2

Adam Robinson
Adam Robinson

Reputation: 185643

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?

Upvotes: 0

Dave Markle
Dave Markle

Reputation: 97701

"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".

Upvotes: 9

Related Questions