using System;
using System.ComponentModel;
using System.Net;
using System.Runtime.InteropServices;
namespace SteamWare
{
///
/// Helper x gestione accesso device di rete
///
public class NetworkConnection : IDisposable
{
string _networkName;
///
/// Init classe
///
///
///
public NetworkConnection(string networkName, NetworkCredential credentials)
{
_networkName = networkName;
var netResource = new NetResource()
{
Scope = ResourceScope.GlobalNetwork,
ResourceType = ResourceType.Disk,
DisplayType = ResourceDisplaytype.Share,
RemoteName = networkName
};
var userName = string.IsNullOrEmpty(credentials.Domain)
? credentials.UserName
: string.Format(@"{0}\{1}", credentials.Domain, credentials.UserName);
var result = WNetAddConnection2(
netResource,
credentials.Password,
userName,
0);
if (result != 0)
{
throw new Win32Exception(result);
}
}
///
/// Dispose classe
///
~NetworkConnection()
{
Dispose(false);
}
///
/// Dispose classe
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// Dispose classe
///
///
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);
}
///
///
///
[StructLayout(LayoutKind.Sequential)]
public class NetResource
{
///
/// Scope delal risorsa
///
public ResourceScope Scope;
///
/// TIpo di risorsa
///
public ResourceType ResourceType;
///
/// Tipèo display
///
public ResourceDisplaytype DisplayType;
///
/// Impiego
///
public int Usage;
///
/// Nome locale
///
public string LocalName;
///
/// Nome remoto
///
public string RemoteName;
///
/// Commento
///
public string Comment;
///
/// Provider
///
public string Provider;
}
///
/// Enum Scope della risorsa
///
public enum ResourceScope : int
{
///
/// connessa
///
Connected = 1,
///
/// rete globale
///
GlobalNetwork,
///
/// memorizzata
///
Remembered,
///
/// recente
///
Recent,
///
/// contesto
///
Context
};
///
/// Enum tipo res
///
public enum ResourceType : int
{
///
/// Qualsiasi
///
Any = 0,
///
/// Disco
///
Disk = 1,
///
/// Printer
///
Print = 2,
///
/// Riservato
///
Reserved = 8,
}
///
/// Enum tipo display
///
public enum ResourceDisplaytype : int
{
///
/// Generico
///
Generic = 0x0,
///
/// Dominio
///
Domain = 0x01,
///
/// Server
///
Server = 0x02,
///
/// COndivisione
///
Share = 0x03,
///
/// File
///
File = 0x04,
///
/// Gruppo
///
Group = 0x05,
///
/// Rete
///
Network = 0x06,
///
/// Radice
///
Root = 0x07,
///
/// Share admin
///
Shareadmin = 0x08,
///
/// Cartella
///
Directory = 0x09,
///
/// Albero
///
Tree = 0x0a,
///
/// NDS container
///
Ndscontainer = 0x0b
}
}