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
?
第二種:
}
本文轉自 夢在旅途 博客園博客,原文鏈接:http://www.cnblogs.com/zuowj/archive/2013/05/11/3072724.html ,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的C#访问远程共享加锁文件夹的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 二十年后的回眸(5)——一部单车闯天下
- 下一篇: 网络工程师必懂的专业术语