1

I have a class library that is being run as [user a] within an application. The application needs to obtain files (their names and their content) from a network locatation that has been set up as a share. The share is on a Windows environment and on the same domain on which the application is running.

Running Application: User account: [user a] Domain: myDomain

Network Share: User able to access: [user b] Share: \192.168.1.1\folder Domain: myDomain

I need, within the application, to connect to \192.168.1.1\folder\folder with my files\ and obtain the file names and their content. A simple Directory.GetFiles should be ok (if that is possible).

I have looked at some answers online which talk about NetworkCredentials but that is being passed to web requests. I just want to use it in a standard directory IO listing and collect the file content.

I feel like there should be a way to do this without having to use someone's project that is 400 lines long - surely I can do this with .NET really easily and I just don't know which class to use.

Cheers,

IvanH
  • 5,039
  • 14
  • 60
  • 81
Jake Mills
  • 11
  • 1

1 Answers1

0

Like this:

#region WNetUseConnection

[DllImport("Mpr.dll", EntryPoint = "WNetUseConnection", CallingConvention = CallingConvention.Winapi)]
private static extern int WNetUseConnection(IntPtr hwndOwner, NETRESOURCE lpNetResource, string lpPassword, string lpUserID, int dwFlags, string lpAccessName, string lpBufferSize, string lpResult);

[DllImport("Mpr.dll", EntryPoint = "WNetCancelConnection2", CallingConvention = CallingConvention.Winapi)]
private static extern int WNetCancelConnection2(string lpName, int dwFlags, bool fForce);

[StructLayout(LayoutKind.Sequential)]
private class NETRESOURCE
{
    public ResourceScope dwScope = 0;
    public ResourceType dwType = 0;
    public ResourceDisplayType dwDisplayType = 0;
    public ResourceUsage dwUsage = 0;
    public string lpLocalName = null;
    public string lpRemoteName = null;
    public string lpComment = null;
    public string lpProvider = null;
};

public enum ResourceScope
{
    RESOURCE_CONNECTED = 1,
    RESOURCE_GLOBALNET,
    RESOURCE_REMEMBERED,
    RESOURCE_RECENT,
    RESOURCE_CONTEXT
};

public enum ResourceType
{
    RESOURCETYPE_ANY,
    RESOURCETYPE_DISK,
    RESOURCETYPE_PRINT,
    RESOURCETYPE_RESERVED
};

public enum ResourceUsage
{
    RESOURCEUSAGE_CONNECTABLE = 0x00000001,
    RESOURCEUSAGE_CONTAINER = 0x00000002,
    RESOURCEUSAGE_NOLOCALDEVICE = 0x00000004,
    RESOURCEUSAGE_SIBLING = 0x00000008,
    RESOURCEUSAGE_ATTACHED = 0x00000010,
    RESOURCEUSAGE_ALL = (RESOURCEUSAGE_CONNECTABLE | RESOURCEUSAGE_CONTAINER | RESOURCEUSAGE_ATTACHED),
};

public enum ResourceDisplayType
{
    RESOURCEDISPLAYTYPE_GENERIC,
    RESOURCEDISPLAYTYPE_DOMAIN,
    RESOURCEDISPLAYTYPE_SERVER,
    RESOURCEDISPLAYTYPE_SHARE,
    RESOURCEDISPLAYTYPE_FILE,
    RESOURCEDISPLAYTYPE_GROUP,
    RESOURCEDISPLAYTYPE_NETWORK,
    RESOURCEDISPLAYTYPE_ROOT,
    RESOURCEDISPLAYTYPE_SHAREADMIN,
    RESOURCEDISPLAYTYPE_DIRECTORY,
    RESOURCEDISPLAYTYPE_TREE,
    RESOURCEDISPLAYTYPE_NDSCONTAINER
};

#endregion WNetUseConnection

And here is how to use it: (It is enough if you do this once, you don't have to repeat it before every access to the share.)

// Initialize connection to file share
NETRESOURCE nr = new NETRESOURCE();
nr.dwType = ResourceType.RESOURCETYPE_DISK;
nr.lpRemoteName = "\\192.168.1.1";

string user = "user B";
string password = "password for user B";

WNetUseConnection(IntPtr.Zero, nr, password, user, 0, null, null, null);
Christophe
  • 354
  • 3
  • 16
  • I just noticed that I myself, when I had that same question, did find this code here: [link](http://stackoverflow.com/a/1441845/3318781) – Christophe Feb 01 '16 at 12:24