tnblog
首页
视频
资源
登录

.Net 磁盘映射内网远程共享文件夹

2061人阅读 2023/11/16 8:54 总访问:3467283 评论:0 收藏:0 手机
分类: .net后台框架

.netcore

.Net 磁盘映射内网远程共享文件夹

  1. public class WNetHelper
  2. {
  3. public WNetHelper() { }
  4. [DllImport("mpr.dll", EntryPoint = "WNetAddConnection2")]
  5. private static extern uint WNetAddConnection2(NetResource lpNetResource, string lpPassword, string lpUsername, uint dwFlags);
  6. [DllImport("Mpr.dll", EntryPoint = "WNetCancelConnection2")]
  7. private static extern uint WNetCancelConnection2(string lpName, uint dwFlags, bool fForce);
  8. [StructLayout(LayoutKind.Sequential)]
  9. public class NetResource
  10. {
  11. public int dwScope;
  12. public int dwType;
  13. public int dwDisplayType;
  14. public int dwUsage;
  15. public string lpLocalName;
  16. public string lpRemoteName;
  17. public string lpComment;
  18. public string lpProvider;
  19. }
  20. /// <summary>
  21. /// 为网络共享做本地映射
  22. /// </summary>
  23. /// <param name="username">访问用户名(windows系统需要加计算机名,如:comp-1\user-1)</param>
  24. /// <param name="password">访问用户密码</param>
  25. /// <param name="remoteName">网络共享路径(如:\\192.168.0.9\share)</param>
  26. /// <param name="localName">本地映射盘符</param>
  27. /// <returns></returns>
  28. public static uint WNetAddConnection(string username, string password, string remoteName, string localName)
  29. {
  30. NetResource netResource = new NetResource();
  31. netResource.dwScope = 2;
  32. netResource.dwType = 1;
  33. netResource.dwDisplayType = 3;
  34. netResource.dwUsage = 1;
  35. netResource.lpLocalName = localName;
  36. netResource.lpRemoteName = remoteName.TrimEnd('\\');
  37. uint result = WNetAddConnection2(netResource, password, username, 0);
  38. return result;
  39. }
  40. public static uint WNetCancelConnection(string name, uint flags, bool force)
  41. {
  42. uint nret = WNetCancelConnection2(name, flags, force);
  43. return nret;
  44. }
  45. }


调用方式

  1. // 用户名 密码 远程路径 本地盘符
  2. state = WNetHelper.WNetAddConnection(user, pwd, @"\\hznetapp1\hrs-cfa", "P:");
  3. if (state.Equals(0))
  4. {
  5. _logger.LogInformation($"创建驱动成功:{state} P:\\Jobs存在不? {Directory.Exists("P:\\Jobs")}");
  6. //创建共享目录的上传路径
  7. if (Directory.Exists("P:\\Jobs"))
  8. {
  9. _logger.LogInformation("创建驱动成功:" + state.ToString());
  10. }
  11. }
  12. else
  13. {
  14. _logger.LogInformation("添加网络驱动器错误,错误号:" + state.ToString());
  15. }

欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739

评价

剑轩

2019/8/22 10:15:02

c语言很厉害,各种东西都可以写

net core 使用 EF Code First

下面这些内容很老了看这篇:https://www.tnblog.net/aojiancc2/article/details/5365 项目使用多层,把数据库访问...

cAPS.net 保存base64位格式的图片

publicvoidUpload() { //取出图片对应的base64位字符 stringimgBase=Request[&quot;imgBase&quot;]; //c#里边的base6...

Quartz.net实例动态改变周期调度。misfire、Cron

Quartz:Java编写的开源的任务调度作业框架 类似Timer之类定时执行的功能,但是更强大Quartz.NET:是把Quartz转成C# NuGet...

.net Windows服务发布、安装、卸载、监听脚本。服务调试

一、脚本 为方便不用每次都去写安装卸载的脚本1.安装脚本@echooff @echo开始安装【服务】 %SystemRoot%\Microsoft.NET\Fr...

c、VB.net中全角半角转换方法

///&lt;summary&gt; ///转全角的函数(SBCcase) ///&lt;/summary&gt; ///&lt;paramname=&quot;input&quot;&gt;任意字符串...

.net mvc分部页,.net core分部页

.net分部页的三种方式第一种:@Html.Partial(&quot;_分部页&quot;)第二种:@{ Html.RenderPartial(&quot;分部页&quot;);}...

C.net 配合小程序实现经过第三方服务器中转文件

某些时候,微信小程序前段上传文件的时候需要经过第三方服务器再将文件上传到客户的服务器;操作如下:1:(小程序内向中端服...

.net实现QQ邮箱发送邮件功能

1、微软已经帮我们封装好了发送邮件的类MailMessage,MailMessage类构造一些邮件信息,然后通过SmtpClient进行邮件发送。Mai...

StackExchange.Redis操作redis(net core支持)

官方git开源地址https://github.com/StackExchange/StackExchange.Redis官方文档在docs里边都是官方的文档通过nuget命令下...

windows 自带的netsh进行端口映射

使用netsh 把本地任意ip的25566端口 映射到192.168.81.234的25565端口netshinterfaceportproxyaddv4tov4listenaddress=0.0....

确保.net程序始终以管理员身份运行

usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; ...

ASP.net Timer细节处理

Timer的用法:1:本人称之为计时器,是asp.net官方的一种。用法即是计时所用 2:关于计时有很多中方式,本人学识有限,暂...

.net core 使用session

tip:net core 2.2后可以直接启用session了,不用在自己添加一次session依赖,本身就添加了使用nuget添加引用Microsoft.AspN...

通俗易懂,什么是.net?什么是.net Framework?什么是.net core?

朋友圈@蓝羽 看到一篇文章写的太详细太通俗了,搬过来细细看完,保证你对.NET有个新的认识理解原文地址:https://www.cnblo...

asp.net core2.0 依赖注入 AddTransient与AddScoped的区别

asp.net core主要提供了三种依赖注入的方式其中AddTransient与AddSingleton比较好区别AddTransient瞬时模式:每次都获取一...

asp.net主动推送百度seo

虽然可以使用百度提供的js自动推送,但是估计度娘还是希望主动推送一点。哈哈^_^,女孩子嘛大多都喜欢被动一点。publicclass...
这一世以无限游戏为使命!
排名
2
文章
634
粉丝
44
评论
93
docker中Sware集群与service
尘叶心繁 : 想学呀!我教你呀
一个bug让程序员走上法庭 索赔金额达400亿日元
叼着奶瓶逛酒吧 : 所以说做程序员也要懂点法律知识
.net core 塑形资源
剑轩 : 收藏收藏
映射AutoMapper
剑轩 : 好是好,这个对效率影响大不大哇,效率高不高
ASP.NET Core 服务注册生命周期
剑轩 : http://www.tnblog.net/aojiancc2/article/details/167
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术