简体   繁体   English

从UNC路径获取需要用户名和密码的文件

[英]Obtain files from UNC path that requires a username and password

I have a class library that is being run as [user a] within an application. 我有一个在[应用程序a]中作为[用户a]运行的类库。 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. 共享位于Windows环境中,并且在运行该应用程序的同一域中。

Running Application: User account: [user a] Domain: myDomain 正在运行的应用程序:用户帐户:[user a]域:myDomain

Network Share: User able to access: [user b] Share: \\192.168.1.1\\folder Domain: myDomain 网络共享:用户可以访问:[用户b]共享:\\ 192.168.1.1 \\ folder域: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. 我需要在应用程序中,使用我的文件连接到\\ 192.168.1.1 \\ folder \\ folder \\,并获取文件名及其内容。 A simple Directory.GetFiles should be ok (if that is possible). 一个简单的Directory.GetFiles应该可以(如果可能的话)。

I have looked at some answers online which talk about NetworkCredentials but that is being passed to web requests. 我在网上看了一些有关NetworkCredentials的答案,但这些答案已传递给Web请求。 I just want to use it in a standard directory IO listing and collect the file content. 我只想在标准目录IO列表中使用它并收集文件内容。

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. 我觉得应该有一种方法,而不必使用某人的长达400行的项目-我肯定可以很轻松地使用.NET进行此操作,而我只是不知道使用哪个类。

Cheers, 干杯,

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM