如何删除目录中的所有Xlsx文件(名称不同) [英] How Can I Delete All The Xlsx Files In A Directory Except One(The Name Is Different)

查看:125
本文介绍了如何删除目录中的所有Xlsx文件(名称不同)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在目录main中有十个文件
例如:fileA.xlsx,fileB.xlsx,FileC.xlsx,testFile.xlsx都在Main目录

i想要存档并删除除testFile.xlsx之外的这些文件。

testFile应保留在Main目录中。



非常感谢你的帮助

Hi,
I have ten files in directory main
for example: fileA.xlsx, fileB.xlsx, FileC.xlsx, testFile.xlsx are all in Main directory
i want to archive and delete these files except the testFile.xlsx.
testFile should stay in the directory Main.

Will appreciate your help

推荐答案

我甚至不知道这是否会编译(我现在在我的Mac上) - 但我会尝试一些东西喜欢



I dont even know if this will compile (Im on my Mac at the moment) - but I'd be trying something like

// Needs uses System.IO;

string folderPath = "Your File Folder Here";

if (Directory.Exists(folderPath))
{
	DirectoryInfo di = new DirectoryInfo(folderPath);

     // Get only the xlsx files excluding One We Don’t Want
     List<string> xlsxFiles = di.GetFiles("*.xlsx")
                              .Where(file => !(file.Name.EndsWith("testFile.xlsx")))
                              .Select(file => file.Name).ToList();

	foreach (string filenameToDelete in xlsxFiles)
	{
                // Archive File Goes Here
                // Then Delete It 
		File.Delete(filenameToDelete);
	}
}


这篇关于如何删除目录中的所有Xlsx文件(名称不同)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆