繁体   English   中英

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

[英]WNetAddConnection2 Remote Access and read the directory files in c#

我必须从Intranet上的远程服务器读取文件“abc.txt”。 我使用WNetAddConnection2来做到这一点。 使用此stackoverflow链接此链接 现在我成功了。 当我尝试使用远程连接时,它仍然指向我的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) 
                { 

                }
            } 
        }

我究竟做错了什么? 需要帮助请叫我。

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

}

暂无
暂无

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

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