简体   繁体   English

如何删除下载文件夹中的文件

[英]How to delete files in the Downloads folder

I've managed to use the WinAPI SHGetKnownFolderPath() method to get the path to the Downloads folder, but when using the following code, I can't get it to delete specific types of files:我已经设法使用 WinAPI SHGetKnownFolderPath()方法来获取下载文件夹的路径,但是在使用以下代码时,我无法删除特定类型的文件:

string rootFolderPath = KnownFolders.GetPath(KnownFolder.Downloads);
**string filesToDelete = @"*Agreement, CCRPCI, SECCI, Debit*.pdf";**
string[] fileList = System.IO.Directory.GetFiles(rootFolderPath, filesToDelete);
foreach(string file in fileList)
{
    System.IO.File.Delete(file);
}

What do I need to add / change in order to reference multiple different file names?我需要添加/更改什么才能引用多个不同的文件名? I know for a fact the bit I've highlighted in bold is incorrect.我知道我用粗体突出显示的那一点是不正确的。

EDITED -编辑 -

Is there a way to delete files with different file names but the same extension type?有没有办法删除文件名不同但扩展名类型相同的文件? As for the reason behind why I need to do this - My automation tests download various different files which get saved into the downloads folder.至于我为什么需要这样做的原因 - 我的自动化测试下载了各种不同的文件,这些文件被保存到下载文件夹中。 As the volume of automation tests is high these documents start to take up a lot of storage.由于自动化测试的数量很大,这些文档开始占用大量存储空间。 Currently I'm going in and manually deleting them.目前我正在进入并手动删除它们。 I'm very new to coding guys and to this forum, I know it's a lot to ask but please have patience with me as I'm only trying to learn.我对编码人员和这个论坛很陌生,我知道有很多问题要问,但请耐心等待我,因为我只是想学习。 (Already had two down votes) (已经有两票反对)

Is there a way to delete files with different file names but the same extension type?有没有办法删除文件名不同但扩展名类型相同的文件?

You can use the shell commands, del DownloadsPath\\\\*.xxx and run it in your program.您可以使用 shell 命令del DownloadsPath\\\\*.xxx并在您的程序中运行它。 And here is the method to run shell commands in C#. 是在 C# 中运行 shell 命令的方法。

Or run a bat file directly.或者直接运行一个bat文件。

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

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