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.Diag开发者_如何学Gonostics.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.
"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"
.
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();
}
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?
精彩评论