如何通过Process运行delete命令?

时间:2011-02-10 05:17:15

标签: c# class process class-library

这不起作用,它找不到del.exe ......

        Process p = new Process();
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.FileName = "del.exe";
        p.StartInfo.Arguments = "*.bak";
        p.Start();
        p.Close();

2 个答案:

答案 0 :(得分:3)

你这样做是错误的。您应该使用File.Delete method代替。

示例代码:

string sourceDir = @"C:\Backups";   // change this to the location of the files
string[] bakList = Directory.GetFiles(sourceDir, "*.bak");

try
{
    foreach (string f in bakList)
    {
        File.Delete(f);
    }
}
catch (IOException ioex)
{
    // failed to delete because the file is in use
}
catch (UnauthorizedAccessException uaex)
{
    // failed to delete because file is read-only,
    // or user doesn't have permission
}

答案 1 :(得分:0)

如果您有理由选择执行Directory.GetFiles处理File.Delete加上处理?