0

I'm working with the .NET Compact Framework 3.5 and want to delete some specific folders and their subfolders. When I run the app it gives IO exception. I've tried to use Directory.Delete(path) method but it didn't work.

How can I solve this problem?

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(){
         myfunc();
         MessageBox.Show("Cihaz resetlendi!");
      }

      public static void myfunc()
      {
          string mainPath = @"\Storage Card\deneme";

          try
          {
              DeleteDirectory(mainPath + "CRM");
              DeleteDirectory(mainPath + "BHTS");
              DeleteDirectory(mainPath + "IMAGES");
              DeleteDirectory(mainPath + "STYLES");
              DeleteDirectory(mainPath + "TABLES");
              DeleteDirectory(mainPath + "LOG");

              File.Delete(mainPath + "Agentry.ini");
              File.Delete(mainPath + "Agentry.app");
              File.Delete(mainPath + "Agentry.usr");
          }
          catch (IOException e)
          {
              myfunc();
          }
      }

      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);
      }
   }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Ok, i can delete everything what i want now. As summary ; I need to delete - 5folders (which has subfolders) and 3files - in one directory. I can delete files by using File.Delete() method and can delete directories by using recursive method. Here is the main problem ; i cant do both two steps. – serkan_demir Jan 03 '14 at 14:58
  • Change your line: `string mainPath = @"\Storage Card\deneme";` by `string mainPath = @"\Storage Card\deneme\";` as said you miss a "\" – Nicolas R Jan 07 '14 at 12:28

3 Answers3

1

Why not delete the directory recursively:

Directory.Delete(path, true);

See here.

Also, see here as it may be similar to what you are encountering.

Community
  • 1
  • 1
acarlon
  • 16,764
  • 7
  • 75
  • 94
0

Try this..

var dir = new DirectoryInfo(@FolderPath);
dir.Delete(true);
Sunny
  • 219
  • 1
  • 10
0

You are not telling what kind of IO exception you are getting, Are you missing a backslash () in your path?

mainPath + "CRM" becomes "\Storage Card\denemeCRM" and not "\Storage Card\deneme\CRM"
Ankush Madankar
  • 3,689
  • 4
  • 40
  • 74
artokai
  • 405
  • 3
  • 8