使用NetUseDel时,c#UNC路径仍然有效

时间:2013-02-25 18:17:03

标签: .net unc net-use

我正在使用NetUse创建与UNC路径的连接以枚举目录。在,NetUseDelete,我需要立即开始发生未经授权的例外。目前,似乎NetUseDelete的效果已被删除,至少在.NET中是这样。以下单元测试演示了应该发生的事情。

// 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()没有抛出错误。

如果我设置断点并等待一段时间,GetDirectories()将按预期抛出异常。

我检查了“net use”,并且在GetDirectories()传递时没有建立连接,这是不正确的。如果“net use”显示没有连接,则GetDirectories()应该失败。

有什么想法吗? .NET中是否有一些我可以清除的缓存?

0 个答案:

没有答案