51 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			51 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			C#
		
	
	
| using System.Runtime.InteropServices;
 | |
| 
 | |
| namespace IRaCIS.Core.Domain.Share
 | |
| {
 | |
|     [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;
 | |
|     }
 | |
| 
 | |
| 
 | |
|     public class WNetAddConnectionHelper
 | |
|     {
 | |
|         [DllImport("mpr.dll", EntryPoint = "WNetAddConnection2")]
 | |
|         private static extern uint WNetAddConnection2(NetResource lpNetResource, string lpPassword, string lpUsername, uint dwFlags);
 | |
| 
 | |
|         [DllImport("mpr.dll")]
 | |
|         public static extern int WNetCancelConnection2A(string sharename, int dwFlags, int fForce);
 | |
| 
 | |
|         public static void Connect()
 | |
|         {
 | |
| 
 | |
|             var remoteName = @"\\192.168.1.119\Potomac_01";
 | |
|             NetResource netResource = new NetResource();
 | |
|             netResource.dwScope = 1;
 | |
|             netResource.dwType = 1;
 | |
|             netResource.dwDisplayType = 3;
 | |
|             netResource.dwUsage = 1;
 | |
|             netResource.lpLocalName = "W:";
 | |
|             netResource.lpRemoteName = remoteName.TrimEnd('\\');
 | |
| 
 | |
|             WNetAddConnection2(netResource, "Everest@suzhou406", "share", 1);
 | |
| 
 | |
|         }
 | |
| 
 | |
|         public static int Disconnect()
 | |
|         {
 | |
|             return WNetCancelConnection2A("W:", 1, 1);
 | |
|         }
 | |
| 
 | |
|     }
 | |
| }
 | |
| 
 |