Windows 7 x64 SP1 .NET 框架 3.5 SP1
我写了简单的代码,但它会随着时间的推移而起作用,异常发生在每一秒通过。...即:它适用于偶数开始:2、4、6、8 等,但我得到奇数开始的例外:1、3、5、7、9 等
// localMenuDirName is 'GPSM\AdminCAD'.
DirectoryInfo menuDir = new DirectoryInfo(Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.Programs), localMenuDirName));
if (menuDir.Exists) {
FileInfo[] files = menuDir.GetFiles("*", SearchOption.AllDirectories);
foreach (FileInfo file in files) {
file.IsReadOnly = false;
}
sb.AppendLine(String.Format("We begin deleting the '{0}' directory", menuDir.FullName));
Directory.Delete(menuDir.FullName, true); // Get Exception here
// menuDir.Delete(true); // here I get same exception.
输出文本:
我们开始删除“C:\Users\andrey.bushman\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\GPSM\AdminCAD”目录
例外:目录不为空。
但是目录是空的(所有文件都已删除)。我打开资源管理器并看到它。
下一个代码总是可以正常工作:
// localMenuDirName is 'GPSM\AdminCAD'.
DirectoryInfo menuDir = new DirectoryInfo(Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.Programs), localMenuDirName));
if (menuDir.Exists) {
FileInfo[] files = menuDir.GetFiles("*", SearchOption.AllDirectories);
foreach (FileInfo file in files) {
file.IsReadOnly = false;
}
sb.AppendLine(String.Format("We begin deleting the '{0}' directory", menuDir.FullName));
try {
Directory.Delete(menuDir.FullName, true);
}
catch {
// Try again... Now it works without exception!
Directory.Delete(menuDir.FullName, true);
}
sb.AppendLine("Operation was executed successfully.");
为什么会发生?