Results 1 to 4 of 4

Thread: How to remove a directory in C#

  1. #1
    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.

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    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];

  3. #3
    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 ) ;

  4. #4
    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.

Similar Threads

  1. Replies: 1
    Last Post: 13-04-2011, 06:24 PM
  2. Replies: 5
    Last Post: 22-05-2010, 07:33 AM
  3. cannot remove folder the directory is not empty
    By dazzareds in forum Windows Software
    Replies: 1
    Last Post: 12-10-2009, 09:35 PM
  4. How to make PHP remove a Directory
    By Adamaris in forum Software Development
    Replies: 3
    Last Post: 29-06-2009, 09:28 PM
  5. How To remove directory in linux
    By Vespera in forum Operating Systems
    Replies: 3
    Last Post: 16-05-2009, 02:41 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,748,470.09465 seconds with 16 queries