Advertisement
uwekeim

NetworkConnection.cs

Nov 28th, 2022
810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.26 KB | Software | 0 0
  1. namespace Tools
  2. {
  3.     using System;
  4.     using System.Diagnostics;
  5.     using System.IO;
  6.     using System.Runtime.InteropServices;
  7.  
  8.     /// <summary>
  9.     /// Wrapper for the "NET USE" functionality.
  10.     /// </summary>
  11.     public class NetworkConnection :
  12.         IDisposable
  13.     {
  14.         private string _networkName;
  15.  
  16.         public NetworkConnection(
  17.             string networkName,
  18.             bool activate,
  19.             string userName,
  20.             string password)
  21.         {
  22.             if (activate)
  23.             {
  24.                 doNetUse(networkName, userName, password);
  25.             }
  26.         }
  27.  
  28.         private void doNetUse(string networkName, string userName, string password)
  29.         {
  30.             var netResource = new NetResource
  31.             {
  32.                 Scope = ResourceScope.GlobalNetwork,
  33.                 ResourceType = ResourceType.Disk,
  34.                 DisplayType = ResourceDisplaytype.Share,
  35.                 RemoteName = networkName
  36.             };
  37.  
  38.             var result = WNetAddConnection2(
  39.                 netResource,
  40.                 password,
  41.                 userName,
  42.                 0);
  43.  
  44.             if (result != 0)
  45.             {
  46.                 throw new IOException(string.Format("Error connecting to remote share '{0}': {1}", networkName, result), result);
  47.             }
  48.  
  49.             _networkName = networkName;
  50.         }
  51.  
  52.         ~NetworkConnection()
  53.         {
  54.             doDispose();
  55.         }
  56.  
  57.         public void Dispose()
  58.         {
  59.             doDispose();
  60.             GC.SuppressFinalize(this);
  61.         }
  62.  
  63.         private void doDispose()
  64.         {
  65.             if (!string.IsNullOrEmpty(_networkName))
  66.             {
  67.                 var result = WNetCancelConnection2(_networkName, 0, true);
  68.                 Trace.TraceInformation("Result for canceling network connection: {0}.", result);
  69.             }
  70.         }
  71.  
  72.         [DllImport(@"mpr.dll")]
  73.         private static extern int WNetAddConnection2(
  74.             NetResource netResource,
  75.             string password,
  76.             string username,
  77.             int flags);
  78.  
  79.         [DllImport(@"mpr.dll")]
  80.         private static extern int WNetCancelConnection2(
  81.             string name,
  82.             int flags,
  83.             bool force);
  84.     }
  85.  
  86.     [StructLayout(LayoutKind.Sequential)]
  87.     public class NetResource
  88.     {
  89.         public ResourceScope Scope;
  90.         public ResourceType ResourceType;
  91.         public ResourceDisplaytype DisplayType;
  92.         public int Usage;
  93.         public string LocalName;
  94.         public string RemoteName;
  95.         public string Comment;
  96.         public string Provider;
  97.     }
  98.  
  99.     public enum ResourceScope : int
  100.     {
  101.         Connected = 1,
  102.         GlobalNetwork,
  103.         Remembered,
  104.         Recent,
  105.         Context
  106.     };
  107.  
  108.     public enum ResourceType : int
  109.     {
  110.         Any = 0,
  111.         Disk = 1,
  112.         Print = 2,
  113.         Reserved = 8,
  114.     }
  115.  
  116.     public enum ResourceDisplaytype : int
  117.     {
  118.         Generic = 0x0,
  119.         Domain = 0x01,
  120.         Server = 0x02,
  121.         Share = 0x03,
  122.         File = 0x04,
  123.         Group = 0x05,
  124.         Network = 0x06,
  125.         Root = 0x07,
  126.         Shareadmin = 0x08,
  127.         Directory = 0x09,
  128.         Tree = 0x0a,
  129.         Ndscontainer = 0x0b
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement