Click here to Skip to main content
16,012,107 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
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();
            }
        }


Regards
Balamurugan
Posted
Updated 22-Oct-13 0:10am
v4
Comments
[no name] 21-Oct-13 8:37am    
can you please explain more of your requirement. in which language you want the solution kind of.
Balamurugan1989 21-Oct-13 8:40am    
In C#.net i need it.I have created the file and it will saved inside the project. I have mention the path now when im clicking the delete tat particular files and folders should delete.

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

Or:
C#
Directory.Delete(@"C:\Bala\Code on October 21\Project\bin\Debug\Data", true
 
Share this answer
 
Comments
Balamurugan1989 21-Oct-13 9:46am    
I was getting this error The process cannot access the file 'C:\Bala\Code on October 21\Project\bin\Debug\DB\k.db' because it is being used by another process. i have not opened or used this process anywhere.
OriginalGriff 21-Oct-13 10:53am    
What have you done with it? How did you copy it? Have you tried without VS open?
Balamurugan1989 21-Oct-13 11:12am    
In my file menu delete project is available when i'm clicking this the project should delete which saved in the system.
OriginalGriff 21-Oct-13 11:18am    
What does that have to do with C#?
You are confusing me now - remember, I can't see your screen - I only get what you tell me...
Balamurugan1989 23-Oct-13 8:28am    
After long struggle i itself found the solution....
After long struggle i myself found the code and executed.
Inside the Menu click created these method and executed.
C#
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);
   }
}
 
Share this answer
 
v3
Comments
johannesnestler 23-Oct-13 9:05am    
You should consider creating you paths with Path.Combine and use the other System.IO.Path functions to get filename from path etc.
Balamurugan1989 23-Oct-13 10:06am    
Instead of that i have used Directory.GetFiles...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900