2016-06-01 3 views
1

인트라넷의 원격 서버에서 "abc.txt"파일을 읽어야합니다. WNetAddConnection2를 사용했습니다. Used this stackoverflow linkthis link too. 이제는 성공으로 연결했습니다. 원격 연결을 사용하려고해도 내 C 드라이브를 계속 가리 킵니다. 나는 방금 만든 리모컨을 사용하여 연결하고 거기에서 파일을 가져오고 싶습니다.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) 
       { 

       } 
      } 
     } 

내가 뭘 잘못하고 있니? 네가 필요한 것이 있으면 알려줘.

답변

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

}