C # Use wnetaddconnection2a and wnetcancelconnection2a to access shared network folders.
Create a shared folder on the target server and an access account named test;
Public Enum error_id
{
Error_success = 0, // success
Error _ busy = 170,
Error_more_data = 234,
Error_no_browser_servers_found = 6118,
Error_invalid_level = 124,
Error_access_denied = 5,
Error_invalid_password = 86,
Error_invalid_parameter = 87,
Error_bad_dev_type = 66,
Error_not_enough_memory = 8,
Error_network_busy = 54,
Error_bad_netpath = 53,
Error_no_net = 1222,
Error_invalid_handle_state = 1609,
Error_extended_error = 1208,
Error _ device_already_remembered = 1202,
Error_no_net_or_bad_path = 1203
}
Public Enum resource_scope
{
Resource_connected = 1,
Resource_globalnet = 2,
Resource_remembered = 3,
Resource_recent = 4,
Resource_context = 5
}
Public Enum resource_type
{
Resourcetype_any = 0,
Resourcetype_disk = 1,
Resourcetype_print = 2,
Resourcetype_reserved = 8,
}
Public Enum resource_usage
{
Resourceusage_connectable = 1,
Resourceusage_container = 2,
Resourceusage_nolocaldevice = 4,
Resourceusage_sibling = 8,
Resourceusage_attached = 16,
Resourceusage_all = (resourceusage_connectable | resourceusage_container | resourceusage_attached ),
}
Public Enum resource_displaytype
{
Resourcedisplaytype_generic = 0,
Resourcedisplaytype_domain = 1,
Resourcedisplaytype_server = 2,
Resourcedisplaytype_share = 3,
Resourcedisplaytype_file = 4,
Resourcedisplaytype_group = 5,
Resourcedisplaytype_network = 6,
Resourcedisplaytype_root = 7,
Resourcedisplaytype_policadmin = 8,
Resourcedisplaytype_directory = 9,
Resourcedisplaytype_tree = 10,
Resourcedisplaytype_ndscontainer = 11
}
[Structlayout (layoutkind. Sequential)]
Public struct netresource
{
Public resource_scope dwscope;
Public resource_type dwtype;
Public resource_displaytype dwdisplaytype;
Public resource_usage dwusage;
[Financialas (unmanagedtype. lpstr)]
Public String lplocalname;
[Financialas (unmanagedtype. lpstr)]
Public String lpremotename;
[Financialas (unmanagedtype. lpstr)]
Public String lpcomment;
[Financialas (unmanagedtype. lpstr)]
Public String lpprovider;
}
Public class networkconnection
{
[Dllimport ("MIP. dll")]
Public static extern int wnetaddconnection2a (netresource [] lpnetresource, string lppassword, string lpusername, int dwflags );
[Dllimport ("MIP. dll")]
Public static extern int wnetcancelconnection2a (string sharename, int dwflags, int Fforce );
Public static int connect (string remotepath, string localpath, string username, string password)
{
Netresource [] share_driver = new netresource [1];
Resource_driver [0]. dwscope = resource_scope.resource_globalnet;
Resource_driver [0]. dwtype = resource_type.resourcetype_disk;
Resource_driver [0]. dwdisplaytype = resource_displaytype.resourcedisplaytype_share;
Resource_driver [0]. dwusage = resource_usage.resourceusage_connectable;
Performance_driver [0]. lplocalname = localpath;
Performance_driver [0]. lpremotename = remotepath;
Disconnect (localpath );
Int ret = wnetaddconnection2a (share_driver, password, username, 1 );
Return ret;
}
Public static int disconnect (string localpath)
{
Return wnetcancelconnection2a (localpath, 1, 1 );
}
Call method:
Public static void testnetworkconnection ()
{
String localpath = "X :";
// Int status = networkconnection. Connect (@ "\ 192.168.0.2 \ test", localpath, @ "test", "test ");
Int status = networkconnection. Connect ("\\\\ 192.168.0.2 \ test", localpath, @ "test", "test ");
If (status = (INT) error_id.error_success)
{
Filestream FS = new filestream (localpath + @ "\ 123.txt", filemode. openorcreate );
Using (streamwriter stream = new streamwriter (FS ))
{
Stream. writeline ("Hello, succeeded ");
Stream. Flush ();
Stream. Close ();
}
FS. Close ();
}
Else
{
Console. writeline (Status );
}
Networkconnection. Disconnect (localpath );
}
Reference:
Http://support.microsoft.com/kb/173011
Http://support.microsoft.com/kb/842789
This article reposted from csdn blog, http://blog.csdn.net/aoyo/archive/2009/02/26/3939651.aspx