Fix warning (maybe)
This commit is contained in:
@@ -5,106 +5,219 @@ using System.Runtime.InteropServices;
|
||||
|
||||
namespace SteamWare
|
||||
{
|
||||
public class NetworkConnection : IDisposable
|
||||
{
|
||||
string _networkName;
|
||||
|
||||
public NetworkConnection(string networkName, NetworkCredential credentials)
|
||||
/// <summary>
|
||||
/// Helper x gestione accesso device di rete
|
||||
/// </summary>
|
||||
public class NetworkConnection : IDisposable
|
||||
{
|
||||
_networkName = networkName;
|
||||
string _networkName;
|
||||
|
||||
var netResource = new NetResource()
|
||||
{
|
||||
Scope = ResourceScope.GlobalNetwork,
|
||||
ResourceType = ResourceType.Disk,
|
||||
DisplayType = ResourceDisplaytype.Share,
|
||||
RemoteName = networkName
|
||||
};
|
||||
/// <summary>
|
||||
/// Init classe
|
||||
/// </summary>
|
||||
/// <param name="networkName"></param>
|
||||
/// <param name="credentials"></param>
|
||||
public NetworkConnection(string networkName, NetworkCredential credentials)
|
||||
{
|
||||
_networkName = networkName;
|
||||
|
||||
var userName = string.IsNullOrEmpty(credentials.Domain)
|
||||
? credentials.UserName
|
||||
: string.Format(@"{0}\{1}", credentials.Domain, credentials.UserName);
|
||||
var netResource = new NetResource()
|
||||
{
|
||||
Scope = ResourceScope.GlobalNetwork,
|
||||
ResourceType = ResourceType.Disk,
|
||||
DisplayType = ResourceDisplaytype.Share,
|
||||
RemoteName = networkName
|
||||
};
|
||||
|
||||
var result = WNetAddConnection2(
|
||||
netResource,
|
||||
credentials.Password,
|
||||
userName,
|
||||
0);
|
||||
var userName = string.IsNullOrEmpty(credentials.Domain)
|
||||
? credentials.UserName
|
||||
: string.Format(@"{0}\{1}", credentials.Domain, credentials.UserName);
|
||||
|
||||
if (result != 0)
|
||||
{
|
||||
throw new Win32Exception(result);
|
||||
}
|
||||
var result = WNetAddConnection2(
|
||||
netResource,
|
||||
credentials.Password,
|
||||
userName,
|
||||
0);
|
||||
|
||||
if (result != 0)
|
||||
{
|
||||
throw new Win32Exception(result);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dispose classe
|
||||
/// </summary>
|
||||
~NetworkConnection()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
/// <summary>
|
||||
/// Dispose classe
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
/// <summary>
|
||||
/// Dispose classe
|
||||
/// </summary>
|
||||
/// <param name="disposing"></param>
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
WNetCancelConnection2(_networkName, 0, true);
|
||||
}
|
||||
|
||||
[DllImport("mpr.dll")]
|
||||
private static extern int WNetAddConnection2(NetResource netResource,
|
||||
string password, string username, int flags);
|
||||
|
||||
[DllImport("mpr.dll")]
|
||||
private static extern int WNetCancelConnection2(string name, int flags,
|
||||
bool force);
|
||||
}
|
||||
|
||||
~NetworkConnection()
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public class NetResource
|
||||
{
|
||||
Dispose(false);
|
||||
/// <summary>
|
||||
/// Scope delal risorsa
|
||||
/// </summary>
|
||||
public ResourceScope Scope;
|
||||
/// <summary>
|
||||
/// TIpo di risorsa
|
||||
/// </summary>
|
||||
public ResourceType ResourceType;
|
||||
/// <summary>
|
||||
/// Tipèo display
|
||||
/// </summary>
|
||||
public ResourceDisplaytype DisplayType;
|
||||
/// <summary>
|
||||
/// Impiego
|
||||
/// </summary>
|
||||
public int Usage;
|
||||
/// <summary>
|
||||
/// Nome locale
|
||||
/// </summary>
|
||||
public string LocalName;
|
||||
/// <summary>
|
||||
/// Nome remoto
|
||||
/// </summary>
|
||||
public string RemoteName;
|
||||
/// <summary>
|
||||
/// Commento
|
||||
/// </summary>
|
||||
public string Comment;
|
||||
/// <summary>
|
||||
/// Provider
|
||||
/// </summary>
|
||||
public string Provider;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
/// <summary>
|
||||
/// Enum Scope della risorsa
|
||||
/// </summary>
|
||||
public enum ResourceScope : int
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
/// <summary>
|
||||
/// connessa
|
||||
/// </summary>
|
||||
Connected = 1,
|
||||
/// <summary>
|
||||
/// rete globale
|
||||
/// </summary>
|
||||
GlobalNetwork,
|
||||
/// <summary>
|
||||
/// memorizzata
|
||||
/// </summary>
|
||||
Remembered,
|
||||
/// <summary>
|
||||
/// recente
|
||||
/// </summary>
|
||||
Recent,
|
||||
/// <summary>
|
||||
/// contesto
|
||||
/// </summary>
|
||||
Context
|
||||
};
|
||||
/// <summary>
|
||||
/// Enum tipo res
|
||||
/// </summary>
|
||||
public enum ResourceType : int
|
||||
{
|
||||
WNetCancelConnection2(_networkName, 0, true);
|
||||
/// <summary>
|
||||
/// Qualsiasi
|
||||
/// </summary>
|
||||
Any = 0,
|
||||
/// <summary>
|
||||
/// Disco
|
||||
/// </summary>
|
||||
Disk = 1,
|
||||
/// <summary>
|
||||
/// Printer
|
||||
/// </summary>
|
||||
Print = 2,
|
||||
/// <summary>
|
||||
/// Riservato
|
||||
/// </summary>
|
||||
Reserved = 8,
|
||||
}
|
||||
/// <summary>
|
||||
/// Enum tipo display
|
||||
/// </summary>
|
||||
public enum ResourceDisplaytype : int
|
||||
{
|
||||
/// <summary>
|
||||
/// Generico
|
||||
/// </summary>
|
||||
Generic = 0x0,
|
||||
/// <summary>
|
||||
/// Dominio
|
||||
/// </summary>
|
||||
Domain = 0x01,
|
||||
/// <summary>
|
||||
/// Server
|
||||
/// </summary>
|
||||
Server = 0x02,
|
||||
/// <summary>
|
||||
/// COndivisione
|
||||
/// </summary>
|
||||
Share = 0x03,
|
||||
/// <summary>
|
||||
/// File
|
||||
/// </summary>
|
||||
File = 0x04,
|
||||
/// <summary>
|
||||
/// Gruppo
|
||||
/// </summary>
|
||||
Group = 0x05,
|
||||
/// <summary>
|
||||
/// Rete
|
||||
/// </summary>
|
||||
Network = 0x06,
|
||||
/// <summary>
|
||||
/// Radice
|
||||
/// </summary>
|
||||
Root = 0x07,
|
||||
/// <summary>
|
||||
/// Share admin
|
||||
/// </summary>
|
||||
Shareadmin = 0x08,
|
||||
/// <summary>
|
||||
/// Cartella
|
||||
/// </summary>
|
||||
Directory = 0x09,
|
||||
/// <summary>
|
||||
/// Albero
|
||||
/// </summary>
|
||||
Tree = 0x0a,
|
||||
/// <summary>
|
||||
/// NDS container
|
||||
/// </summary>
|
||||
Ndscontainer = 0x0b
|
||||
}
|
||||
|
||||
[DllImport("mpr.dll")]
|
||||
private static extern int WNetAddConnection2(NetResource netResource,
|
||||
string password, string username, int flags);
|
||||
|
||||
[DllImport("mpr.dll")]
|
||||
private static extern int WNetCancelConnection2(string name, int flags,
|
||||
bool force);
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public class NetResource
|
||||
{
|
||||
public ResourceScope Scope;
|
||||
public ResourceType ResourceType;
|
||||
public ResourceDisplaytype DisplayType;
|
||||
public int Usage;
|
||||
public string LocalName;
|
||||
public string RemoteName;
|
||||
public string Comment;
|
||||
public string Provider;
|
||||
}
|
||||
|
||||
public enum ResourceScope : int
|
||||
{
|
||||
Connected = 1,
|
||||
GlobalNetwork,
|
||||
Remembered,
|
||||
Recent,
|
||||
Context
|
||||
};
|
||||
|
||||
public enum ResourceType : int
|
||||
{
|
||||
Any = 0,
|
||||
Disk = 1,
|
||||
Print = 2,
|
||||
Reserved = 8,
|
||||
}
|
||||
|
||||
public enum ResourceDisplaytype : int
|
||||
{
|
||||
Generic = 0x0,
|
||||
Domain = 0x01,
|
||||
Server = 0x02,
|
||||
Share = 0x03,
|
||||
File = 0x04,
|
||||
Group = 0x05,
|
||||
Network = 0x06,
|
||||
Root = 0x07,
|
||||
Shareadmin = 0x08,
|
||||
Directory = 0x09,
|
||||
Tree = 0x0a,
|
||||
Ndscontainer = 0x0b
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user