C # implements access to network shared folders, using WNETADDCONNECTION2A and WNETCANCELCONNECTION2A.
Set up a shared folder on the target server, set up access account 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_network = 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_shareadmin = 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;
[MarshalAs (UNMANAGEDTYPE.LPSTR)]
public string lpLocalName;
[MarshalAs (UNMANAGEDTYPE.LPSTR)]
public string Lpremotename;
[MarshalAs (UNMANAGEDTYPE.LPSTR)]
public string lpcomment;
[MarshalAs (UNMANAGEDTYPE.LPSTR)]
public string Lpprovider;
}
public class Networkconnection
{
[DllImport ("Mpr.dll")]
public static extern int wnetaddconnection2a (netresource[] lpnetresource, String Lppassword, string lpusername, int dwfla GS);
[DllImport ("Mpr.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];
Share_driver[0].dwscope = Resource_scope. Resource_globalnet;
Share_driver[0].dwtype = Resource_type. Resourcetype_disk;
Share_driver[0].dwdisplaytype = Resource_displaytype. Resourcedisplaytype_share;
Share_driver[0].dwusage = resource_usage. resourceusage_connectable;
Share_driver[0].lplocalname = LocalPath;
Share_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 Mode:
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, Success");
Stream. Flush ();
Stream. Close ();
}
Fs. Close ();
}
Else
{
Console.WriteLine (status);
}
Networkconnection.disconnect (LocalPath);
}
Reference Documentation:
http://support.microsoft.com/kb/173011
http://support.microsoft.com/kb/842789