繁体   English   中英

如何将File.Delete函数过滤为某些文件扩展名?

[英]How to filter File.Delete function to certain file extensions?

我在c#中使用以下函数删除文件夹中的所有内容; 但是,我想知道是否可以添加到此功能中以仅删除文件扩展名为“ .dll”的文件。

Array.ForEach(Directory.GetFiles(DIRname), File.Delete);

谢谢。

您可以使用重载的方法Directory.GetFiles(string,string)将SearchPattern作为*.dll传递,以获取所有扩展名为.dll的文件

Directory.GetFiles()

返回与指定目录中的指定搜索模式匹配的文件名(包括其路径)。

尝试这个:

Directory.GetFiles(DIRname,"*.dll")

File.Delete不支持任何通配符。

您可以使用以下两个选项之一(使用“ * .dll”通配符,可以根据需要进行更改)。

获取所有文件并使用Parallel.ForEach并行删除它们,这比正常循环快得多。

    var files = Directory.GetFiles(@"c:\mypath", "*.dll");
    Parallel.ForEach(files, (f)=> File.Delete(f));

或者,将Windows命令外壳程序与DEL命令一起使用:

    Process process = new Process()
    {
        StartInfo = new ProcessStartInfo()
        {
            UseShellExecute = false,
            RedirectStandardOutput = true,
            CreateNoWindow = true,
            RedirectStandardInput = true,
            RedirectStandardError = true,
            FileName = "cmd.exe",
            Arguments = @"/C del c:\mypath\*.dll",

        }
    };
    process.Start();
    process.WaitForExit();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM