如何通过Process运行删除命令?

发布于 2024-10-17 03:35:46 字数 254 浏览 2 评论 0原文

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

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

This does not work, it cant find del.exe...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伏妖词 2024-10-24 03:35:46

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

示例代码:

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
}

You're doing it the wrong way. You should be using the File.Delete method instead.

Sample code:

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
}
山川志 2024-10-24 03:35:46

如果您出于某种原因选择通过 Directory.GetFiles< 执行 Process /code>加上 File .删除

If there a reason you're choosing to execute Process over Directory.GetFiles coupled with File.Delete?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文