博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#访问远程共享加锁文件夹
阅读量:7082 次
发布时间:2019-06-28

本文共 3092 字,大约阅读时间需要 10 分钟。

第一种:

using System;

using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ManageCenter

{
    public  class ConnShareRes
    {
        private string userName;
        private string userPwd;
        private string shareResDictionary;
        //构造函数
        public ConnShareRes(string myUserName, string myUserPwd, string myShareResDictionary)
        {
            this.userName = myUserName;
            this.userPwd = myUserPwd;
            this.shareResDictionary = myShareResDictionary;
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct NETRESOURCEA
        {
            public int dwScope;
            public int dwType;
            public int dwDisplayType;
            public int dwUsage;
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpLocalName;
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpRemoteName;
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpComment;
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpProvider;
            public override String ToString()
            {
                String str = "LocalName: " + lpLocalName + " RemoteName: " + lpRemoteName + " Comment: " + lpComment + " lpProvider: " + lpProvider;
                return (str);
            }
        }
        [DllImport("mpr.dll")]
        public static extern int WNetAddConnection2([MarshalAs(UnmanagedType.LPArray)] NETRESOURCEA[] lpNetResource, [MarshalAs(UnmanagedType.LPStr)] string lpPassword, [MarshalAs(UnmanagedType.LPStr)] string UserName, int dwFlags);
        [DllImport("mpr.dll")]
        public static extern int WNetCancelConnection2(string lpName, int dwFlags, bool fForce);
        //开始远程连接
        public  bool RemoteConnect(bool bConnected)
        {
            int res;
            NETRESOURCEA[] n = new NETRESOURCEA[1];
            n[0] = new NETRESOURCEA();
            n[0].dwType = 1;
            int dwFlags = 1; // CONNECT_INTERACTIVE;
            //n[0].lpLocalName = @"X:";
            n[0].lpLocalName = @"";

            n[0].lpRemoteName = shareResDictionary;

            //n[0].lpRemoteName = @"";
            n[0].lpProvider = null;
            //Console.WriteLine(n[0]);
            if (bConnected)
            {
                res = WNetAddConnection2(n, userPwd, userName, dwFlags);
            }
            else
            {
                res=WNetCancelConnection2(shareResDictionary, 1, true);
            }
            return (res == 0) ? true : false;
        }
    }//class
}//namespace

 

第二种:

using   System;      using   System.Management;         class   Sample_ConnectionOptions     {             public   static   int   Main(string[]   args)   {                     ConnectionOptions   options   =   new   ConnectionOptions();                     options.Username   =   用户名;   //could   be   in   domain\user   format                     options.Password   =   密码;                     ManagementScope   scope   =   new   ManagementScope(                             "\\\\servername\\root\\cimv2",                             options);                     try   {                             scope.Connect();                             ManagementObject   disk   =   new   ManagementObject(                                     scope,                                     new   ManagementPath("Win32_logicaldisk='c:'"),                                     null);                             disk.Get();                     }                     catch   (Exception   e)   {                             Console.WriteLine("Failed   to   connect:   "   +   e.Message);                     }                     return   0;             }     }

转载地址:http://pwlml.baihongyu.com/

你可能感兴趣的文章
★路由递归查询方法及相关图示【转载】
查看>>
SAP 开源 SCA 工具,扫描软件包依赖漏洞
查看>>
Oracle 中 Object_iD 和 Data_Object_ID 的区别
查看>>
10年前的《武林外传》
查看>>
Nginx源码分析1--------编写Nginx扩展模块
查看>>
动效设计的物理法则:动画的一切皆在于时间点和空间幅度(转)
查看>>
重构——2内联函数(Inline Method)
查看>>
Mysql学习笔记(七)查(补充)
查看>>
在Windows系统中安装集成的PHP开发环境
查看>>
Face++ 论文解读:一种新的行人重识别度量学习方法 | PaperDaily #20
查看>>
VSTO之旅系列(一):VSTO入门
查看>>
从零开始学.net多线程系列(三)——同步
查看>>
CSS垂直居中总结
查看>>
[20170123]db_unique_name与大小写2.txt
查看>>
oracle事务(转)
查看>>
ThinkPHP 的模型使用详细介绍--模型的核心(七)
查看>>
我们正在创建一个被真实需求驱动的手机世界
查看>>
Win10中virtualbox新建虚拟机不能设置64位系统解决
查看>>
Knockout应用开发指南 第五章:创建自定义绑定
查看>>
linux网络配置相关命令、虚拟网络接口eth0:0
查看>>