I am using NetUse to create a connection to a UNC path to enumerate the directories. Upon, NetUseDelete, I need unauthorized exceptions to start happening immediately. Currently, it seems that NetUseDelete's effect is deleted, at least in .NET. The following unit test demonstrates what SHOULD happen.
// 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());
That last Assert fails because GetDirectories() doesn't thrown error.
If I set a breakpoint and wait a bit, GetDirectories() will throw an exception as expected.
I have checked "net use" and there are no connections established when GetDirectories() pass, which is incorrect. GetDirectories() should fail if "net use" shows no connections.
Any ideas? Is there some cache in .NET that I can clear?