Win7系统访问共享文件夹记不住密码(记不住凭据)

1、控制面板——>凭据管理器——>添加Windows凭据;

2、输入要访问的设备名称、用户名、密码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果共享文件夹需要使用账户密码才能访问,你可以使用 NetworkCredential 类来指定访问共享文件夹的账户和密码。例如: ``` string path = @"\\COMPUTER_NAME\SHARED_FOLDER\file.txt"; NetworkCredential credential = new NetworkCredential("USERNAME", "PASSWORD"); FileInfo file = new FileInfo(path); // 使用账户密码访问文件 using (new NetworkConnection(Path.GetDirectoryName(path), credential)) { string content = File.ReadAllText(file.FullName); } ``` 其中,NetworkConnection 是一个自定义类,用于创建网络连接并使用指定的凭据进行身份验证。你需要在代码中添加以下代码: ``` class NetworkConnection : IDisposable { string _networkName; public NetworkConnection(string networkName, NetworkCredential credentials) { _networkName = networkName; var netResource = new NetResource() { Scope = ResourceScope.GlobalNetwork, ResourceType = ResourceType.Disk, DisplayType = ResourceDisplaytype.Share, RemoteName = networkName }; var result = WNetAddConnection2(netResource, credentials.Password, credentials.UserName, 0); if (result != 0) { throw new Win32Exception(result); } } ~NetworkConnection() { Dispose(false); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { WNetCancelConnection2(_networkName, 0, true); } [DllImport("mpr.dll")] private static extern int WNetAddConnection2(NetResource netResource, string password, string username, int flags); [DllImport("mpr.dll")] private static extern int WNetCancelConnection2(string name, int flags, bool force); } [StructLayout(LayoutKind.Sequential)] public class NetResource { public ResourceScope Scope; public ResourceType ResourceType; public ResourceDisplaytype DisplayType; public int Usage; public string LocalName; public string RemoteName; public string Comment; public string Provider; } public enum ResourceScope : int { Connected = 1, GlobalNetwork, Remembered, Recent, Context }; public enum ResourceType : int { Any = 0, Disk = 1, Print = 2, Reserved = 8, } public enum ResourceDisplaytype : int { Generic = 0x0, Domain = 0x01, Server = 0x02, Share = 0x03, File = 0x04, Group = 0x05, Network = 0x06, Root = 0x07, Shareadmin = 0x08, Directory = 0x09, Tree = 0x0a, Ndscontainer = 0x0b } ``` 需要注意的是,这段代码使用了 WNetAddConnection2 和 WNetCancelConnection2 API 函数来创建和取消网络连接。因此,你需要在代码中添加 `using System.Runtime.InteropServices;` 来引用该命名空间。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值