Solved

Delete Files in folder

Posted on 2006-11-09
6
157 Views
Last Modified: 2010-04-16
Hi,
 I need a function that get as parameter a Folder path name and deletes all the files in this folder.

it's 0 if it deleted every thins else it's returns 1


thanks
0
Comment
Question by:madmare
6 Comments
 
LVL 8

Expert Comment

by:sabeesh
ID: 17905610
System.IO.Directory.Delete(stringpath,bool )
0
 
LVL 16

Expert Comment

by:muzzy2003
ID: 17905872
That will delete the directory as well, not what was wanted. Try this:

using System.IO;

public int DeleteFromDirectory(string path)
{
    bool deleted = false;
    while (Directory.GetFiles(path).Count > 0)
    {
        File.Delete(Directory.GetFiles(path)[0]);
        deleted = true;
    }
    return deleted ? 1 : 0;
}
0
 
LVL 16

Expert Comment

by:muzzy2003
ID: 17905874
Swap the 1 and 0 - sorry!
0
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 17905969
Altenative, using exception handling:

using System.IO;
...
public int DeleteFiles(string dirPath)
{
      bool allDeleted = true;
      foreach (string filePath in Directory.GetFiles(dirPath))
      {
            try
            {
                  File.Delete(filePath);
            }
            catch
            {
                  allDeleted = false;
            }
      }
      return allDeleted? 0 : 1;
}
0
 
LVL 28

Accepted Solution

by:
strickdd earned 500 total points
ID: 17906103
This is VB code I converted on the fly, so it may have some syntax errors, but it is close to what you want. Read the comments for more features.

private bool RemoveOldFiles(string DirPath As String)
{
        string[] FileList = Directory.GetFiles(DirPath)
        int FileCount = Directory.GetFiles(DirPath).Length
        int NumDeleted
       
        foreach(string FilePath in FileList)
        {
            string[] TempArr = FilePath.Split("\");
            string FileName = TempArr[TempArr.Length - 1];
           
           DialogResult Selection = MessageBox.Show("Are you sure you want to delete: " & FilePath & "?", "Confirm Deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if( Selection == DialogResult.Yes)
                {
                    'If the file is read-only, set to archive to allow deletion
                    if (File.GetAttributes(FilePath) && FileAttributes.ReadOnly)
                        File.SetAttributes(FilePath, FileAttributes.Archive);

                    File.Delete(FilePath);
                    NumDeleted ++;
                }
           }

        }

       if(FileCount == NumDeleted)
       {
            return true; //change to 1 and 0 if you want, but change the function return type
       }
       else
       {
             return false;
        }
//FROM HERE ON IT LOOPS THROUGH SUBDIRS AND DELETES THEIR FILES
//You'll have to modify this some if you want to use it
/*        string[] SubDirList = Directory.GetDirectories(DirPath);

        foreach(string SubDirPath in SubDirList)
        {
                string[] Temp = SubDirPath.Split("\");
               
                RemoveOldFiles(SubDirPath);
         }*/
         
}


    End Sub
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Video Player 12 27
Calculate number of nights between two dates 5 53
Where is this file? 3 25
C# Linq - Join two objects into one 3 13
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question