NetUse を使用して UNC パスへの接続を作成し、ディレクトリを列挙しています。NetUseDelete の際に、すぐに発生し始めるには、許可されていない例外が必要です。現在、少なくとも .NET では、NetUseDelete の効果は削除されているようです。次の単体テストは、何が起こるべきかを示しています。
// delete every net connection to ensure clean test
var share = @"\\234.234.234.234\share";
var username = "user...";
var password = "password...";
// to begin with, access should be denied
Assert.Throws<UnauthorizedAccessException>(() => new DirectoryInfo(share).GetDirectories());
// the using statement calls NetUseDel when the connection is disposed off.
using (var connection = _networkBrowserService.GetUncConnection())
{
// calls NetUse
if (connection.NetUseWithCredentials(share, username, null, password))
{
// succeeded!
// this should not throw an error!
new DirectoryInfo(share).GetDirectories();
}
else
{
Assert.Fail("Couldn't authenticate username/password");
}
}
// now that we disposed our connection, we should get access denied again
Assert.Throws<UnauthorizedAccessException>(() => new DirectoryInfo(share).GetDirectories());
GetDirectories() がエラーをスローしないため、最後の Assert は失敗します。
ブレークポイントを設定して少し待つと、期待どおりに GetDirectories() が例外をスローします。
「ネット使用」を確認しましたが、GetDirectories() が渡されたときに確立された接続がありません。これは正しくありません。「ネット使用」が接続を示さない場合、GetDirectories() は失敗するはずです。
何か案は?クリアできる .NET のキャッシュはありますか?