Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 13-11-2009
Member
 
Join Date: Nov 2009
Posts: 36
How to remove a directory in C#

How to remove a directory? I am stucked. What I do:

The string is my_test = @ "c:\my folder\my subfolder\dir1\dir2\myfile.txt";

In case 1: I would like to remove from myfile.txt, dir1 and all files and subfolder

In case 1: I would always delete myfile.txt from dir2 and all the files ..

That must now surely be simple, but since I am newbie I need your help.
Reply With Quote
  #2  
Old 13-11-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: How to remove a directory in C#

Is actually quite simple. It is used System.IO. Then you have two methods to delete. First delete files from other directories. Necessarily if you delete the folders you delete the files they contain.

Deleting a file:
Code:
PATH = @"c:\my folder\my subfolder\dir1\dir2\myfile.txt";
File.Delete(PATH);
Remove directory:
Code:
PATH = @"c:\my folder\my subfolder\dir1\";
Directory.Delete(PATH);
After assuming full path, you must excerpts directory path, for example through the split.
Code:
String _Dir1= PATH.Split('\\')[3];
Reply With Quote
  #3  
Old 13-11-2009
Member
 
Join Date: Nov 2005
Posts: 1,323
Re: How to remove a directory in C#

Quote:
Originally Posted by kelfro View Post
Remove directory:
Code:
PATH = @"c:\my folder\my subfolder\dir1\";
Directory.Delete(PATH);
to delete a directory and all subdirectory and all files it contains, I think we should add a 2nd parameter

Code:
Directory. Delete ( PATH, true ) ;
Reply With Quote
  #4  
Old 13-11-2009
Member
 
Join Date: Nov 2008
Posts: 1,192
Re: How to remove a directory in C#

To find the folder containing a file:

Code:
System.IO.FileInfo fi = new System.IO.FileInfo(fichier);
MessageBox.Show(fi.Directory.FullName);
To find the parent folder of a directory:

Code:
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(fi.Directory.FullName);
MessageBox.Show(di.FullName);
After you juggle these 2 functions depending on the desired level of deletion with System.IO.Directory.Delete (directory, true).

The second parameter of type Boolean indicates recursion.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to remove a directory in C#"
Thread Thread Starter Forum Replies Last Post
How to remove External Drive from computer if 'Safely Remove Hardware' disappears Author Operating Systems 1 13-04-2011 06:24 PM
The version of the Active Directory schema of the source forest is not compatible with the version of Active Directory on this computer Blaine Active Directory 5 22-05-2010 07:33 AM
cannot remove folder the directory is not empty dazzareds Windows Software 1 12-10-2009 09:35 PM
How to make PHP remove a Directory Adamaris Software Development 3 29-06-2009 09:28 PM
How To remove directory in linux Vespera Operating Systems 3 16-05-2009 02:41 PM


All times are GMT +5.5. The time now is 12:31 AM.