Click here to Skip to main content
15,888,521 members
Articles / Programming Languages / C#
Article

CleanUp Files using C Sharp

Rate me:
Please Sign up or sign in to vote.
1.27/5 (9 votes)
22 Jan 2001 90.4K   903   26   7
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


    Written By
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    QuestionWhy reinvent the wheel? Pin
    Eric VanWieren23-Oct-07 8:51
    Eric VanWieren23-Oct-07 8:51 
    I am confused. Why not just use the other functionality of the Directory.Delete function in the System.IO space.

    Directory.Delete (String, Boolean) -- Deletes the specified directory and, if indicated, any subdirectories in the directory.
    Supported by the .NET Compact Framework.

    From MSDN Documentation.

    You might have to go through the directory once. Test the item for file or directory, but no need for recursion. Recursion is only something that should be used if there is no other way around it. It is a shame that recursion is considered to be the end all be all of solutions when it has a dramatic drain on system resources the more it ends up being called.

    - Eric

    It is the nature of man to rise to greatness if greatness is expected of him.
    - John Steinbeck

    Questionshankar Narayan Where are you ? Pin
    Software_Specialist4-Jun-07 3:03
    Software_Specialist4-Jun-07 3:03 
    GeneralDeleting files on WIN98 SE Pin
    Jaysen1-Dec-03 20:09
    Jaysen1-Dec-03 20:09 
    Questionwhat is winforms Pin
    sakuragi10-Oct-03 14:47
    sakuragi10-Oct-03 14:47 
    GeneralCompile Pin
    HTempelman8-Jul-03 10:25
    HTempelman8-Jul-03 10:25 
    GeneralA Simpler Solution Pin
    Anonymous17-May-03 17:34
    Anonymous17-May-03 17:34 
    GeneralRe: A Simpler Solution Pin
    mcaouette30-Apr-04 4:34
    mcaouette30-Apr-04 4:34 

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

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