Click here to Skip to main content
Click here to Skip to main content
Go to top

CleanUp Files using C Sharp

, 22 Jan 2001
Rate this:
Please Sign up or sign in to vote.
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

    Share

    About the Author

    Shankara Narayanan

    United States United States
    No Biography provided

    Comments and Discussions

     
    QuestionWhy reinvent the wheel? PinmemberEric Van Wieren23-Oct-07 8:51 
    Questionshankar Narayan Where are you ? PinmemberSoftware_Specialist4-Jun-07 3:03 
    GeneralDeleting files on WIN98 SE PinsussJaysen Naicker1-Dec-03 20:09 
    Questionwhat is winforms Pinmembersakuragi10-Oct-03 14:47 
    GeneralCompile PinmemberHTempelman8-Jul-03 10:25 
    GeneralA Simpler Solution PinsussAnonymous17-May-03 17:34 
    GeneralRe: A Simpler Solution Pinmembermcaouette30-Apr-04 4:34 

    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.

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