删除目录中的选定文件和文件夹 [英] Deleting the Selcted files and folder in directory

查看:59
本文介绍了删除目录中的选定文件和文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经保存了项目中的文件和文件夹,即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);
   }
}


这篇关于删除目录中的选定文件和文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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