-1

Im working on compact framework 3.5. I can delete files by using File.Delete() and can delete directories by using my method.

Here is the problem : I can call only File.Delete() methods or my directory deleting method (which name is DD). They both works but not work together. Here is my code :

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Reset_Client
{
static class Program
{
    static void Main()
    {
        string path = @"\Storage Card\deneme\";



        File.Delete(path + "Agentry.ini");
        File.Delete(path + "Agentry.app");
        File.Delete(path + "Agentry.usr");
        //DF(path);
        DD(path);


        MessageBox.Show("Cihaz resetlendi!");
    }

    public static void DD(string mainPath)
    {
        try
        {
            DeleteDirectory(mainPath + "CRM");
            DeleteDirectory(mainPath + "BHTS");
            DeleteDirectory(mainPath + "IMAGES");
            DeleteDirectory(mainPath + "STYLES");
            DeleteDirectory(mainPath + "TABLES");
            DeleteDirectory(mainPath + "LOG");

        }
        catch (IOException e)
        {
            DD(mainPath);
        }
    }
    //public static void DF(string mainPath)
    //{
    //    try
    //    {
    //        DeleteFile(mainPath + "Agentry.ini");
    //        DeleteFile(mainPath + "Agentry.app");
    //        DeleteFile(mainPath + "Agentry.usr");
    //    }
    //    catch (IOException e)
    //    {
    //        DF(mainPath);
    //    }
    //}


    public static void DeleteDirectory(string target_dir)
    {
        FileInfo fileInfo = new FileInfo(target_dir);
        FileAttributes attributes = fileInfo.Attributes;

        if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
        {
            // set the attributes to nonreadonly
            fileInfo.Attributes &= ~FileAttributes.ReadOnly;
        }

        string[] files = Directory.GetFiles(target_dir);
        string[] dirs = Directory.GetDirectories(target_dir);

        foreach (string file in files)
        {
            File.Delete(file);
        }

        foreach (string dir in dirs)
        {
            DeleteDirectory(dir);
        }

        Directory.Delete(target_dir, false);
    }

    public static void DeleteFile(string target_dir)
    {
        FileInfo fileInfo = new FileInfo(target_dir);
        FileAttributes attributes = fileInfo.Attributes;

        if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
        {
            // set the attributes to nonreadonly
            fileInfo.Attributes &= ~FileAttributes.ReadOnly;
        }



    }
}
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Please define "not work together". – tnw Jan 03 '14 at 15:20
  • In main ; it works if i call only File.Delete(path + "Agentry.ini"); File.Delete(path + "Agentry.app"); File.Delete(path + "Agentry.usr"); it deletes files. or it works if i call only DD(path); but it does not work when i call all of them. – serkan_demir Jan 03 '14 at 15:27
  • Can you expand on "does not work" - does it throw an exception, what's the error message, etc? – Owen Pauling Jan 03 '14 at 15:32
  • No error message , DD method deletes directories but File.Delete() methods dont delete that 3files. – serkan_demir Jan 03 '14 at 15:41

3 Answers3

0

The following may be your problem.

Typically you can only delete empty directories. Your DD function would have to recurse down the directory path, find all the files, and call File.Delete on them, before deleting the directory that enclosed them.

Floris
  • 45,857
  • 6
  • 70
  • 122
0

you can use Directory.Delete(target_dir, true) replace your DD method to delete directory directly. The second arg is true,means recursive. Another point. If any IO exception occurs. The DD method catch the exception and call DD method again. It will be a dead loop.

RyanWang
  • 178
  • 9
0

Here is a snippit I have. I cannot remember if I got it from somewhere or if I wrote it. This should do what you are wanting to do

 public void ClearFolder(string folderName)  
 {  
   DirectoryInfo dir = new DirectoryInfo(folderName);  
   foreach (FileInfo fi in dir.GetFiles())  
   {  
     fi.Delete();  
   }  
   foreach (DirectoryInfo di in dir.GetDirectories())  
   {  
     ClearFolder(di.FullName);  
     di.Delete();  
   }  
 }  

I would try it like this and see if it works.

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Reset_Client
{
static class Program
{
    static void Main()
    {
        string path = @"\Storage Card\deneme\";
        File.Delete(path + "Agentry.ini");
        File.Delete(path + "Agentry.app");
        File.Delete(path + "Agentry.usr");
        //DF(path);
        DD(path);
        MessageBox.Show("Cihaz resetlendi!");
    }

    public static void DD(string mainPath)
    {
        try
        {
            ClearFolder(mainPath + "CRM");
            ClearFolder(mainPath + "BHTS");
            ClearFolder(mainPath + "IMAGES");
            ClearFolder(mainPath + "STYLES");
            ClearFolder(mainPath + "TABLES");
            ClearFolder(mainPath + "LOG");

        }
        catch (IOException e)
        {
            DD(mainPath);
        }
    }

    public void ClearFolder(string folderName)  
    {  
        DirectoryInfo dir = new DirectoryInfo(folderName);  
        foreach (FileInfo fi in dir.GetFiles())  
        {  
            fi.Delete();  
        }  
        foreach (DirectoryInfo di in dir.GetDirectories())  
        {  
            ClearFolder(di.FullName);  
            di.Delete();  
        }  
    }  
}
}
James B
  • 11
  • 1