Click here to Skip to main content
Licence 
First Posted 22 Jan 2001
Views 76,582
Bookmarked 25 times

CleanUp Files using C Sharp

By | 22 Jan 2001 | Article
Describes the File Access, Persistance with C Sharp
  • Download source files - 35 Kb
  • Introduction

    This article is just a sample application which uses the .Net framework to delete unwanted files from the directories specified. The Sample uses the System.IO to manage the task and uses a recursive function to delete files.

    private void RemoveFiles(string strPath)
    {
        Directory dTemp = new Directory(strPath);
        for(int i = 0; i < lstExts.Items.Count; i++)
        {
            string s = lstExts.Items[i].ToString();
            File[] fileList = dTemp.GetFiles(lstExts.Items[i].ToString());
            for(int j = 0; j < fileList.Length; j++)
            {
                if(fileList[j].IsFile)
                {
                    try
                    {
                        fileList[j].Delete();    
                    } 
                    catch(SecurityException e)
                    {
                        lblStatus.Text = e.Message;
                    }
                    catch(Exception ex)
                    {
                        lblStatus.Text = ex.Message;
                    }
                }
            }
        }
    }
    
    private void EmptyDirectory(string strPath)
    {
        if(!Directory.DirectoryExists(strPath))
            return;
        Directory dirFinder = new Directory(strPath);
        Directory[] dirList = dirFinder.GetDirectories();    
        for(int i = 0; i < dirList.Length; i++)
        {
            string strTemp = strPath + "\\" + dirList[i].Name;
            EmptyDirectory(strTemp);
            RemoveFiles(strTemp);
        }
        RemoveFiles(strPath);
    }
    
    

    The two functions provides the capability of deleting of files recursively. The Classes used in this Project

    • Form
    • File
    • Directory
    • Button
    • ListBox
    • TextBox

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    About the Author

    Shankara Narayanan



    United States United States

    Member



    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    QuestionWhy reinvent the wheel? PinmemberEric Van Wieren8:51 23 Oct '07  
    Questionshankar Narayan Where are you ? PinmemberSoftware_Specialist3:03 4 Jun '07  
    GeneralDeleting files on WIN98 SE PinsussJaysen Naicker20:09 1 Dec '03  
    Questionwhat is winforms Pinmembersakuragi14:47 10 Oct '03  
    GeneralCompile PinmemberHTempelman10:25 8 Jul '03  
    GeneralA Simpler Solution PinsussAnonymous17:34 17 May '03  
    GeneralRe: A Simpler Solution Pinmembermcaouette4:34 30 Apr '04  

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    Permalink | Advertise | Privacy | Mobile
    Web02 | 2.5.120517.1 | Last Updated 23 Jan 2001
    Article Copyright 2001 by Shankara Narayanan
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid