WNetAddConnection2 Remote Access et lisez les fichiers du répertoire dans c #

Je dois lire un fichier “abc.txt” contenu d’un serveur distant sur intranet. J’ai utilisé WNetAddConnection2 pour le faire. Utilisé ce lien stackoverflow et ce lien aussi . Maintenant, j’ai fait le lien comme un succès. Lorsque j’essaie d’utiliser la connexion à distance, elle pointe toujours vers mon lecteur C. Je souhaite que la connexion utilise la connexion distante que je viens de créer et que je récupère des fichiers à partir de là.

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

Qu’est-ce que je fais mal? Fait moi savoir si tu as besoin de quoique ce soit d’autre.

 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  #include  #include  #include  #include #include // 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"\\\\xxxx\\folder"; nr.lpProvider = NULL; // Assign a value to the connection options dwFlags = CONNECT_UPDATE_PROFILE; cancelRetVal = WNetCancelConnection2(L"\\\\xxxx\\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::ssortingng s; std::getline(std::cin, s); exit(1); 

}