0
私はC#アクセス例外の問題があります。ここに私の統合テストは、次のとおりです。System.UnauthorizedAccessException: 'C: ... Local Temp bqrcregv.eww'へのアクセスが拒否されました
public class DirectoryTraverser : IEnumerable<FileInfo>
{
private const string ORDER_FILENAME = "order.mnl";
private readonly IList<FileInfo> order;
public DirectoryTraverser(string dirPath)
{
var orderfilePath = Path.Combine(dirPath, ORDER_FILENAME);
if (new FileInfo(orderfilePath).Exists)
{
order = new List<FileInfo>();
using (var sr = File.OpenText(dirPath))
{
string file;
while ((file = sr.ReadLine()) != null)
order.Add(new FileInfo(Path.Combine(dirPath, file)));
}
}
else
{
order = new List<FileInfo>(new DirectoryInfo(dirPath).GetFiles());
}
}
public IEnumerator<FileInfo> GetEnumerator()
{
return order.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
NUnitのからの出力:
[TestFixture]
public class DirectoryTraverserTest
{
private string testDirectory;
[SetUp]
public void Setup()
{
testDirectory = CreateTempDirectory();
var files = new[] { "file1","file2","file3","file4"};
CreateFiles(testDirectory, files);
System.Console.WriteLine(testDirectory);
}
[TearDown]
public void TearDown()
{
Directory.Delete(testDirectory, true);
}
[Test]
public void testThatFilesComeBackInOrderFileOrder()
{
CreateFile(testDirectory, "order.mnl", "file4\nfile3\nfile2\nfile1\n");
var result = new List<FileInfo>(new DirectoryTraverser(testDirectory));
Assert.That(result[0].FullName, Is.EqualTo(Path.Combine(testDirectory, "file4")));
Assert.That(result[1].FullName, Is.EqualTo(Path.Combine(testDirectory, "file3")));
Assert.That(result[2].FullName, Is.EqualTo(Path.Combine(testDirectory, "file2")));
Assert.That(result[3].FullName, Is.EqualTo(Path.Combine(testDirectory, "file1")));
}
[Test]
public void testThatFilesComeBackInFileSystemOrder()
{
var result = new List<FileInfo>(new DirectoryTraverser(testDirectory));
Assert.That(result[0].FullName, Is.EqualTo(Path.Combine(testDirectory, "file1")));
Assert.That(result[1].FullName, Is.EqualTo(Path.Combine(testDirectory, "file2")));
Assert.That(result[2].FullName, Is.EqualTo(Path.Combine(testDirectory, "file3")));
Assert.That(result[3].FullName, Is.EqualTo(Path.Combine(testDirectory, "file4")));
}
public static string CreateTempDirectory()
{
var path = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(path);
return path;
}
public static void CreateFiles(string path, string[] files)
{
foreach (var file in files)
CreateFile(path, file, file);
}
public static void CreateFile(string path, string name, string content)
{
using(var f = new StreamWriter(Path.Combine(path, name)))
{
f.Write(content);
}
}
}
そしてここでは、テスト対象のクラス(前 テストとしてリファクタリングステップに渡しません)です
Test net.purpletube.minla.test.FileSystem.DirectoryTraverserTest.testThatFilesComeBackInOrderFileOrder'
failed: System.UnauthorizedAccessException : Access to the path 'C:...\Temp\tto2nvka.1es' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
at System.IO.StreamReader..ctor(String path)
at System.IO.File.OpenText(String path)
FileSystem\DirectoryTraverser.cs(21,0): at net.purpletube.minla.FileSystem.DirectoryTraverser..ctor(String
dirPath)
FileSystem\DirectoryTraverserTest.cs(36,0): at net.purpletube.minla.test.FileSystem.DirectoryTraverserTest.testThatFilesComeBackInOrderFileOrder()
1 passed, 1 failed, 0 skipped, took 0.33 seconds (NUnit 2.5.5).
4つのテンポラリファイル(file1〜file4)を読み取ると、 という結果が出ます。問題の原因となる注文ファイルです。
私は完全に失われています。誰かが起こっているかもしれない手掛かりを持っていますか?
パス文字列ではなくディレクトリを 'DirectoryInfo'として渡す別の理由。 –