Here is the code i am using (from ASP.NET) to access shared folder. You
obviously must have User/Password to access that folder.
#region WIN API Declarations
//used in calling WNetAddConnection2
[StructLayout(LayoutKind.Sequential)]
public struct NETRESOURCE
{
public int dwScope;
public int dwType;
public int dwDisplayType;
public int 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;
}
//WIN32API - WNetAddConnection2
[DllImport("mpr.dll",
CharSet = System.Runtime.InteropServices.CharSet.Auto)]
private static extern int WNetAddConnection2A(
[MarshalAs(UnmanagedType.LPArray)] NETRESOURCE[] lpNetResource,
[MarshalAs(UnmanagedType.LPStr)] string lpPassword,
[MarshalAs(UnmanagedType.LPStr)] string lpUserName,
int dwFlags);
[DllImport("mpr.dll",
CharSet = System.Runtime.InteropServices.CharSet.Auto)]
private static extern int WNetCancelConnection2A(
[MarshalAs(UnmanagedType.LPStr)] string lpName,
int dwFlags, int fForce);
#endregion
private byte[] GetFSMSFile(string sFile)
{
NETRESOURCE[] nr = new NETRESOURCE[1];
nr[0].lpRemoteName = _sFSMSShare;
nr[0].lpLocalName = ""; //mLocalName;
nr[0].dwType = 1; //disk
nr[0].dwDisplayType = 0;
nr[0].dwScope = 0;
nr[0].dwUsage = 0;
nr[0].lpComment = "";
nr[0].lpProvider = "";
int iErr = WNetAddConnection2A(nr, _sFSMSShareUserPassword,
_sFSMSShareUser, 0);
if (iErr > 0)
throw new Exception("Can not connect to FSMS share folder");
FileStream st = null;
try
{
st = new FileStream(_sFSMSShare + "\\" + sFile,
FileMode.Open);
int iLen = (int)st.Length;
byte []b = new byte[iLen];
st.Read(b, 0, iLen);
return b;
}
finally
{
if( st != null )
st.Close();
WNetCancelConnection2A(_sFSMSShare, 0, -1);
}
}
George