[StructLayout(LayoutKind.Sequential)]
class NETRESOURCE
{
public int dwScope;
public int dwType;
public int dwDisplayType;
public int dwUsage;
public string LocalName = null;
public string RemoteName = null;
public string Comment = null;
public string Provider = null;
}
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2(NETRESOURCE netResource, string password, string username, int flags);
private void EstablishConnection()
{
NETRESOURCE netResource = new NETRESOURCE();
netResource.dwScope = 2;
netResource.dwType = 1;
netResource.dwDisplayType = 3;
netResource.dwUsage = 1;
netResource.RemoteName = @"//192.168.108.18/fbfilestorage";
int ret = WNetAddConnection2(netResource, "password", @"machinename/username", 0); //the ret value in ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/debug/base/system_error_codes.htm
if (ret != 0)
{
throw new Exception(string.Format("Failed to connect the shared folder:{0}", this.workingFolder));
}
}