程序中访问远程共享文件夹

 #include <windows.h>

#include <stdio.h>
#include <winnetwk.h>
#pragma comment(lib, "Mpr.lib")
void main()
{
NETRESOURCE nr;
DWORD res;
TCHAR szUserName[32] = "test",
szPassword[32] = "test",
szLocalName[32] = "Z:",
szRemoteName[MAX_PATH] = "////192.168.11.64//share";
//
// Assign values to the NETRESOURCE structure.
//
nr.dwScope = RESOURCE_GLOBALNET;
nr.dwType = RESOURCETYPE_ANY;
nr.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE;
nr.dwUsage = RESOURCEUSAGE_CONTAINER;
    
// nr.lpLocalName = szLocalName;   //如果使用这句,则将在 我的电脑 上看到映射的Z:盘图标
nr.lpLocalName = NULL;      //使用这句可以隐藏连接。
    
nr.lpRemoteName = szRemoteName;
nr.lpProvider = NULL;
//
// Call the WNetAddConnection2 function to assign
//   a drive letter to the share.
//
res = WNetAddConnection2(&nr, szPassword, szUserName, FALSE);
//
// If the call succeeds, inform the user; otherwise,
//  print the error.
//
if(res == NO_ERROR)
printf("Connection added /n", szRemoteName);
else
{
printf("Error: %ld/n", res);
exit(-1);
}
printf("/n---begin reading test.cpp on 192.168.11.64/n");
    //隐藏链接时可以这样直接打开远程文件
FILE* fp = fopen("////192.168.11.64//share//test.cpp", "r");
char buf[1024];
while (fgets(buf, 1024, fp))
{
printf("%s", buf);
}
fclose(fp);
printf("---end/n");
char c;
printf("enter 'q' to break connection/n");
while (c = getchar() != 'q')
;
// Call the WNetCancelConnection2 function, specifying
//  that the connection should no longer be a persistent one.
//
DWORD dwResult = WNetCancelConnection2("Z:", 
0, // remove connection from profile 
TRUE);                 // fail if open files or jobs 
return;
}

你可能感兴趣的:(function,File,null,Path,include,FP)