c# - 使用用户名和密码连接到网络驱动器

标签 c# .net

如何提供凭据以便我可以连接到 .NET 中的网络驱动器?

我正在尝试从网络驱动器检索文件,需要提供用户凭据才能访问该驱动器。

最佳答案

非常优雅的解决方案,灵感来自 this one .这个仅使用 .Net 库,不需要使用任何命令行或 Win32 API。

现成引用代码:

NetworkCredential theNetworkCredential = new NetworkCredential(@"domain\username", "password");
CredentialCache theNetCache = new CredentialCache();
theNetCache.Add(new Uri(@"\\computer"), "Basic", theNetworkCredential);
string[] theFolders = Directory.GetDirectories(@"\\computer\share");

关于c# - 使用用户名和密码连接到网络驱动器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3700871/

相关文章:

C# Dictionary<> 和可变键

c# - 使用JSON.NET解析格式为Date(epochTime-offset)的json日期

c# - ASCIIEncoding.GetString(byte[]) 之后表示空字节的字符是什么?

c# - 使用安全 key C# 进行 DES 编码

c# - 使用 Json.Net 序列化时指定自定义 DateTime 格式

c# - 我需要做什么来设置 Visual Studio 以便能够操作 Excel 文件?

c# - Windows窗体继承

c# - 使用 Xamarin Forms 的本地推送通知

c# - ASP.NET MVC/C# - 空合并运算符、类型

c# - 从 URL 下载文件到字符串