
.Net 磁盘映射内网远程共享文件夹
public class WNetHelper
{
public WNetHelper() { }
[DllImport("mpr.dll", EntryPoint = "WNetAddConnection2")]
private static extern uint WNetAddConnection2(NetResource lpNetResource, string lpPassword, string lpUsername, uint dwFlags);
[DllImport("Mpr.dll", EntryPoint = "WNetCancelConnection2")]
private static extern uint WNetCancelConnection2(string lpName, uint dwFlags, bool fForce);
[StructLayout(LayoutKind.Sequential)]
public class NetResource
{
public int dwScope;
public int dwType;
public int dwDisplayType;
public int dwUsage;
public string lpLocalName;
public string lpRemoteName;
public string lpComment;
public string lpProvider;
}
/// <summary>
/// 为网络共享做本地映射
/// </summary>
/// <param name="username">访问用户名(windows系统需要加计算机名,如:comp-1\user-1)</param>
/// <param name="password">访问用户密码</param>
/// <param name="remoteName">网络共享路径(如:\\192.168.0.9\share)</param>
/// <param name="localName">本地映射盘符</param>
/// <returns></returns>
public static uint WNetAddConnection(string username, string password, string remoteName, string localName)
{
NetResource netResource = new NetResource();
netResource.dwScope = 2;
netResource.dwType = 1;
netResource.dwDisplayType = 3;
netResource.dwUsage = 1;
netResource.lpLocalName = localName;
netResource.lpRemoteName = remoteName.TrimEnd('\\');
uint result = WNetAddConnection2(netResource, password, username, 0);
return result;
}
public static uint WNetCancelConnection(string name, uint flags, bool force)
{
uint nret = WNetCancelConnection2(name, flags, force);
return nret;
}
}
调用方式
// 用户名 密码 远程路径 本地盘符
state = WNetHelper.WNetAddConnection(user, pwd, @"\\hznetapp1\hrs-cfa", "P:");
if (state.Equals(0))
{
_logger.LogInformation($"创建驱动成功:{state} P:\\Jobs存在不? {Directory.Exists("P:\\Jobs")}");
//创建共享目录的上传路径
if (Directory.Exists("P:\\Jobs"))
{
_logger.LogInformation("创建驱动成功:" + state.ToString());
}
}
else
{
_logger.LogInformation("添加网络驱动器错误,错误号:" + state.ToString());
}
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739
评价
排名
2
文章
634
粉丝
44
评论
93
docker中Sware集群与service
尘叶心繁 : 想学呀!我教你呀
一个bug让程序员走上法庭 索赔金额达400亿日元
叼着奶瓶逛酒吧 : 所以说做程序员也要懂点法律知识
.net core 塑形资源
剑轩 : 收藏收藏
映射AutoMapper
剑轩 :
好是好,这个对效率影响大不大哇,效率高不高
一个bug让程序员走上法庭 索赔金额达400亿日元
剑轩 : 有点可怕
ASP.NET Core 服务注册生命周期
剑轩 :
http://www.tnblog.net/aojiancc2/article/details/167
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256


欢迎加群交流技术
剑轩
c语言很厉害,各种东西都可以写