• C#访问远程共享加锁文件夹


    第一种:

    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;   
              }   
      }
  • 相关阅读:
    CSS_行内元素和块级元素
    jdbc连接oracle11g的问题——查不出来数据,权限问题
    新的起点
    MVC过滤器详解
    SQL Server游标的使用
    处理百万级以上的数据提高查询速度的方法
    两个有序数组找出相同数据
    C# 可变参数
    C#反射
    产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复。
  • 原文地址:https://www.cnblogs.com/zuowj/p/3072724.html
Copyright © 2020-2023  润新知