如何执行&;del data.txt&;使用过程

本文关键字:txt data 过程 del 何执行 执行 | 更新日期: 2023-09-27 18:02:05

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

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

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

如何使用Process执行del data.txt

如何执行&;del data.txt&;使用过程

您需要在流程中添加"/C"参数。

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

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

p.StartInfo.Arguments = "/C '"del data.txt'"";
相关文章: