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);
}
}
}