C# 如何执行;del data.txt“;使用过程?

C# 如何执行;del data.txt“;使用过程?,c#,C#,下面的简单代码只是一个示例,并不反映我的真实场景。 我试过了,但没用 我想使用Process而不是使用File类删除data.txt using System; using System.Diagnostics; namespace Tester { class Program { static void Main(string[] args) { Process p = new Process();

下面的简单代码只是一个示例,并不反映我的真实场景。 我试过了,但没用

我想使用
Process
而不是使用
File
类删除
data.txt

using System;
using System.Diagnostics;

namespace Tester
{
    class Program
    {
        static void Main(string[] args)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd";
            p.StartInfo.Arguments = "del data.txt";
            p.StartInfo.UseShellExecute=false;
            p.EnableRaisingEvents = true;
            p.Exited += (sender, e) => { Console.WriteLine("Finished"); };

            p.Start();
            p.WaitForExit();
        }
    }
}
如何使用
进程执行
del data.txt

您需要将“/C”参数添加到进程中

p.StartInfo.Arguments = "/C del data.txt";

显然是因为cmd不处理这样的参数。您必须在参数中添加/C(可能还有引号):

p.StartInfo.Arguments = "/C \"del data.txt\"";

@oded直接调用
del
是什么意思?@oded
del
cmd
中的内置命令,不是真正的二进制文件。