Solved

Delete Files in folder

Posted on 2006-11-09
6
156 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

786 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