C# System.Diagnostics.Process-Del命令

C# System.Diagnostics.Process-Del命令,c#,.net,cmd,C#,.net,Cmd,我正在尝试使用System.Diagnostic.Process启动del命令。基本上,我想删除文件名为*.bat的C:\驱动器中的所有内容 System.Diagnostics.Process proc = new System.Diagnostics.Process(); string args = string.Empty; args += "*.bat"; proc.StartInfo.FileName = "del"; proc.StartInfo.WorkingDirectory

我正在尝试使用System.Diagnostic.Process启动del命令。基本上,我想删除文件名为*.bat的C:\驱动器中的所有内容

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

但是,当运行代码时,会引发异常,“系统找不到指定的文件”。我知道该根文件夹中肯定有包含该文件扩展名的文件。

“del”不是可执行文件。它是由命令解释器cmd.exe运行的命令。运行
cmd.exe/c“del foo.txt”
del
不是运行“del”,而是一个控制台命令,而不是可以通过
进程启动的应用程序。您这样做而不使用
System.IO
命名空间中的托管类,有什么原因吗?

您不需要启动del命令。您可以从C#中删除文件


有趣的是,如果找不到.Net功能,我会记住这一点。cmd.exe/c在您希望从Process类运行批处理文件时也很有用。我使用io和其他一些方法,但在用户配置文件之外存在UAC和权限问题。如果我将清单更改为以管理员身份执行,那么它将无法正确检测当前登录的用户以执行我需要执行的任务。
        var files = new DirectoryInfo("C:\\").GetFiles("*.bat");
        foreach (FileInfo fi in files)
        {
            fi.Delete();
        }