【发布时间】:2013-11-26 14:01:36
【问题描述】:
在 System.IO.Directory.Delete(string, bool) (here) 的 MSDN 文档中,它说当“路径引用文件而不是目录”时会抛出 DirectoryNotFoundException。
但是,由于抛出 IOException,以下测试失败:
[Test]
[ExpectedException(typeof(DirectoryNotFoundException))] // because DeleteDirectory fails on files.
public void DeleteFileWithDeleteDirectoryDirectly()
{
var tempPath = Path.Combine(Path.GetTempPath(), "MyTestDirectory");
Directory.CreateDirectory(tempPath);
string file = Path.Combine(tempPath, "File1235.txt");
CreateDummyFile(file);
Assert.That(File.Exists(file));
Directory.Delete(file, true);
}
与
void CreateDummyFile(string name)
{
FileStream fs = File.Open(name, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
fs.WriteByte(255);
fs.Close();
}
(在实际代码中,每次测试后都会删除tempPath,以上为说明用的缩写)。我的强制这个错误的测试是错误的还是文档不正确?
【问题讨论】:
-
此处无法重现。抛出 IOException
-
这就是我要说的。 IOException 被抛出,但 DirectoryNotFoundException 是预期的。