C# WNetAddConnection2远程访问和读取c中的目录文件#

C# WNetAddConnection2远程访问和读取c中的目录文件#,c#,file,server,directory,remote-access,C#,File,Server,Directory,Remote Access,我必须从内联网上的远程服务器读取文件“abc.txt”内容。我用WNetAddConnection2来做这件事。和。现在我成功地建立了联系。当我尝试使用远程连接时,它仍然指向我的C驱动器。我希望连接使用我刚刚建立的远程连接,并从那里获取文件 var oNC = new System.Net.NetworkCredential() { Domain = "192.1.x.y",

我必须从内联网上的远程服务器读取文件“abc.txt”内容。我用WNetAddConnection2来做这件事。和。现在我成功地建立了联系。当我尝试使用远程连接时,它仍然指向我的C驱动器。我希望连接使用我刚刚建立的远程连接,并从那里获取文件

var oNC = new System.Net.NetworkCredential()
                      {
                          Domain = "192.1.x.y",
                          UserName = "localhost\\myremoteadminusername",
                          Password = "myremotepassword"
                      };
        using (new NetworkConnection(@"\\" + "192.1.x.y", oNC))
        {
            String[] sfolderNames = Directory.GetDirectories(oNC.Domain + "\\Logs");
//Get Exception in above line bcoz it somehow points to my local C:\...\\bin\Debug\192.1.x.y\Logs
 //instead of remote 192.1.x.y\Logs

            foreach (String sFolderName in sfolderNames) 
            {
                string[] sarrZipFiles = Directory.GetFiles(sFolderName, "*.txt"); 
                foreach (String sFile in sarrZipFiles) 
                { 

                }
            } 
        }
我做错了什么?如果你还需要什么,请告诉我

这段代码是vc++,用于访问远程资源。也许对你有帮助
This code is vc++, it works for getting access to remote resources. Might help you

#include "stdafx.h"
#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "mpr.lib")

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <Winnetwk.h>
#include<iostream>
#include<string>

// Need to link with Netapi32.lib and Mpr.lib
int _tmain(int argc, _TCHAR* argv[]){  
DWORD dwRetVal;    
NETRESOURCE nr;
DWORD dwFlags;  
DWORD cancelRetVal;

// Zero out the NETRESOURCE struct
memset(&nr, 0, sizeof(NETRESOURCE));

// Assign our values to the NETRESOURCE structure.   
nr.dwType = RESOURCETYPE_ANY;

nr.dwScope = RESOURCE_GLOBALNET;
nr.lpLocalName =NULL;

nr.lpRemoteName = L"\\\\x.x.x.x\\folder";

nr.lpProvider = NULL;

// Assign a value to the connection options
dwFlags = CONNECT_UPDATE_PROFILE;   

cancelRetVal = WNetCancelConnection2(L"\\\\x.x.x.x\\fodler", 0, true);

//usage WNetAddConnection2("location", L"password", L"domain\\username", 0);
dwRetVal = WNetAddConnection2(&nr, L"password", L"domain\\username", 0);

if (dwRetVal == NO_ERROR)
    wprintf(L"Connection added to %s\n", nr.lpRemoteName);
else
    wprintf(L"WNetAddConnection2 failed with error: %u\n", dwRetVal);

std::string s;
std::getline(std::cin, s);
exit(1);
#包括“stdafx.h” #ifndef UNICODE #定义UNICODE #恩迪夫 #pragma注释(lib,“mpr.lib”) #包括 #包括 #包括 #包括 #包括 #包括 //需要与Netapi32.lib和Mpr.lib链接 inttmain(intargc,TCHAR*argv[]){ 德沃德·德雷特瓦尔; 网络资源; 德沃德旗; 德沃德·坎塞雷瓦尔; //将NETRESOURCE结构归零 memset(&nr,0,sizeof(NETRESOURCE)); //将我们的值分配给NETRESOURCE结构。 nr.dwType=RESOURCETYPE\u ANY; nr.dwScope=资源\全球网络; nr.lpLocalName=NULL; nr.lpRemoteName=L“\\\\x.x.x\\folder”; nr.lpProvider=NULL; //为连接选项指定一个值 dwFlags=连接\更新\配置文件; cancelRetVal=WNetCancelConnection2(L“\\\\x.x.x\\fodler”,0,true); //用法WNetAddConnection2(“位置”,L“密码”,L“域\\用户名”,0); dwRetVal=wnetadconnection2(&nr,L“密码”,L“域\\用户名”,0); if(dwRetVal==无错误) wprintf(L“连接已添加到%s\n”,nr.lpRemoteName); 其他的 wprintf(L“wneTadConnection2失败,错误:%u\n”,dwRetVal); std::字符串s; 标准::getline(标准::cin,s); 出口(1);
}

此代码是vc++,用于访问远程资源。也许对你有帮助
#包括“stdafx.h”
#ifndef UNICODE
#定义UNICODE
#恩迪夫
#pragma注释(lib,“mpr.lib”)
#包括
#包括
#包括
#包括
#包括
#包括
//需要与Netapi32.lib和Mpr.lib链接
inttmain(intargc,TCHAR*argv[]){
德沃德·德雷特瓦尔;
网络资源;
德沃德旗;
德沃德·坎塞雷瓦尔;
//将NETRESOURCE结构归零
memset(&nr,0,sizeof(NETRESOURCE));
//将我们的值分配给NETRESOURCE结构。
nr.dwType=RESOURCETYPE\u ANY;
nr.dwScope=资源\全球网络;
nr.lpLocalName=NULL;
nr.lpRemoteName=L“\\\\x.x.x\\folder”;
nr.lpProvider=NULL;
//为连接选项指定一个值
dwFlags=连接\更新\配置文件;
cancelRetVal=WNetCancelConnection2(L“\\\\x.x.x\\fodler”,0,true);
//用法WNetAddConnection2(“位置”,L“密码”,L“域\\用户名”,0);
dwRetVal=wnetadconnection2(&nr,L“密码”,L“域\\用户名”,0);
if(dwRetVal==无错误)
wprintf(L“连接已添加到%s\n”,nr.lpRemoteName);
其他的
wprintf(L“wneTadConnection2失败,错误:%u\n”,dwRetVal);
std::字符串s;
标准::getline(标准::cin,s);
出口(1);
}