删除目录中的选定文件和文件夹

编程入门 行业动态 更新时间:2024-10-28 21:30:29
本文介绍了删除目录中的选定文件和文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经保存了项目中的文件和文件夹,即10月21日的C:\ Bala \ Code.\\Project \bin \Debug \ Data和C: \ Bala \Code于10月21日\ Project \ bin \Debug \DB。现在我需要删除我选择的文件。当我点击删除所有文件被删除。我使用了以下代码,但我只想删除选定的文件和文件夹。

Hi, I have saved the file and folder inside the project i.e C:\Bala\Code on October 21\Project\bin\Debug\Data and C:\Bala\Code on October 21\Project\bin\Debug\DB.Now i need to delete the file which i have selected.When im clicking the delete all the files getting deleted. i have used the below code but i want to delete only selected file and folder.

private void ClearFolder(string FolderName) { DirectoryInfo dir = new DirectoryInfo(Environment.CurrentDirectory + "\\Data\\"); foreach (FileInfo fi in dir.GetFiles()) { fi.IsReadOnly = false; fi.Delete(); } foreach (DirectoryInfo di in dir.GetDirectories()) { ClearFolder(di.FullName); di.Delete(); } }

问候 Balamurugan

Regards Balamurugan

推荐答案

尝试: Try: File.Delete(@"C:\Bala\Code on October 21\Project\bin\Debug\Data\MyFileIWantRidOf.File");

或:

Or:

Directory.Delete(@"C:\Bala\Code on October 21\Project\bin\Debug\Data", true

经过长时间的努力,我自己找到了代码并执行了。 在菜单中单击创建这些方法并执行。 After long struggle i myself found the code and executed. Inside the Menu click created these method and executed. string msg = null; ClearFolder(msg); private void ClearFolder(string FolderName) { string[] delete_files_2DData = null; string[] delete_files_Data = null; string[] datafile = null; try { delete_files_2DData = Directory.GetFiles(Environment.CurrentDirectory + "\\DB\\2DData\\" + Global.CreatedProjectName + "\\"); string file = System.IO.Path.GetDirectoryName(Environment.CurrentDirectory + "\\DB\\2DData\\" + Global.CreatedProjectName + "\\"); datafile = Directory.GetFiles(Environment.CurrentDirectory + "\\Data\\"); string prf = "" + Global.CreatedProjectName; string rname = "" + "Run" + Global.RunOpend; foreach (string data_file in datafile) { string[] datafilpath = data_file.Split('\\'); string[] finalpath = datafilpath.Last().Split('_'); if (finalpath.Contains(prf)) { File.Delete(data_file); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }

更多推荐

删除目录中的选定文件和文件夹

本文发布于:2023-11-12 16:42:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582001.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹   文件   目录中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!