问题描述
大家好,最近做一个Windows服务是监听某光驱,一旦光驱有光盘时自动读取内容,并通过共享文件的方式传输到服务器上。于是在网上找到了WNetAddConnection2的WindowsAPI函数,通过此函数建立磁盘映射,例如:将\192.168.0.1Share文件夹映射成本地磁盘的P:在控制台程序中没有发生问题,映射磁盘和断开映射都非常成功。但是将程序整理到C#写的Windows服务时,在调用WNetAddConnection2总是返回1312值(正常是返回0表示创建成功)不明白为什么发生这种情况,请教版里的各位大神,这是为什么?是因为Windows服务的权限问题吗?还是WNetAddConnection2还依赖别的服务?
解决方案
解决方案二:
有没有人来回答下,我正好也碰到这个问题,求助下
解决方案三:
publicstaticclassDrived{publicenumERROR_ID{ERROR_SUCCESS=0,//SuccessERROR_ACCESS_DENIED=5,ERROR_NOT_ENOUGH_MEMORY=8,ERROR_READ_FAULT=30,Windows_cannot_find_the_network_path=51,ERROR_BAD_NETPATH=53,ERROR_NETWORK_BUSY=54,ERROR_NETWORK_ACCESS_DENIED=65,ERROR_BAD_DEV_TYPE=66,ERROR_BAD_NET_NAME=67,ERROR_REQ_NOT_ACCEP=71,ERROR_ALREADY_ASSIGNED=85,ERROR_INVALID_PASSWORD=86,ERROR_INVALID_PARAMETER=87,ERROR_OPEN_FAILED=110,ERROR_INVALID_LEVEL=124,ERROR_BUSY=170,ERROR_MORE_DATA=234,ERROR_NO_BROWSER_SERVERS_FOUND=6118,ERROR_NO_NETWORK=1222,ERROR_INVALID_HANDLE_STATE=1609,ERROR_EXTENDED_ERROR=1208,ERROR_DEVICE_ALREADY_REMEMBERED=1202,ERROR_NO_NET_OR_BAD_PATH=1203,the_user_has_not_been_granted_the_requested_logon_type_at_this_computer=1385,unknown_user_name_or_bad_password=1326,ERROR_ACCOUNT_RESTRICTION=1327,ERROR_INVALID_WORKSTATION=1329,logon_request_contained_an_invalid_logon_type_value=1367,}publicenumRESOURCE_SCOPE{RESOURCE_CONNECTED=1,RESOURCE_GLOBALNET=2,RESOURCE_REMEMBERED=3,RESOURCE_RECENT=4,RESOURCE_CONTEXT=5}publicenumRESOURCE_TYPE{RESOURCETYPE_ANY=0,RESOURCETYPE_DISK=1,RESOURCETYPE_PRINT=2,RESOURCETYPE_RESERVED=8,}publicenumRESOURCE_USAGE{RESOURCEUSAGE_CONNECTABLE=1,RESOURCEUSAGE_CONTAINER=2,RESOURCEUSAGE_NOLOCALDEVICE=4,RESOURCEUSAGE_SIBLING=8,RESOURCEUSAGE_ATTACHED=16,RESOURCEUSAGE_ALL=(RESOURCEUSAGE_CONNECTABLE|RESOURCEUSAGE_CONTAINER|RESOURCEUSAGE_ATTACHED),}publicenumRESOURCE_DISPLAYTYPE{RESOURCEDISPLAYTYPE_GENERIC=0,RESOURCEDISPLAYTYPE_DOMAIN=1,RESOURCEDISPLAYTYPE_SERVER=2,RESOURCEDISPLAYTYPE_SHARE=3,RESOURCEDISPLAYTYPE_FILE=4,RESOURCEDISPLAYTYPE_GROUP=5,RESOURCEDISPLAYTYPE_NETWORK=6,RESOURCEDISPLAYTYPE_ROOT=7,RESOURCEDISPLAYTYPE_SHAREADMIN=8,RESOURCEDISPLAYTYPE_DIRECTORY=9,RESOURCEDISPLAYTYPE_TREE=10,RESOURCEDISPLAYTYPE_NDSCONTAINER=11}[StructLayout(LayoutKind.Sequential)]publicstructNETRESOURCE{publicRESOURCE_SCOPEdwScope;publicRESOURCE_TYPEdwType;publicRESOURCE_DISPLAYTYPEdwDisplayType;publicRESOURCE_USAGEdwUsage;[MarshalAs(UnmanagedType.LPStr)]publicstringlpLocalName;[MarshalAs(UnmanagedType.LPStr)]publicstringlpRemoteName;[MarshalAs(UnmanagedType.LPStr)]publicstringlpComment;[MarshalAs(UnmanagedType.LPStr)]publicstringlpProvider;}publicclassNetworkConnection{[DllImport("mpr.dll")]publicstaticexternintWNetAddConnection2(NETRESOURCE[]lpNetResource,stringlpPassword,stringlpUserName,intdwFlags);[DllImport("mpr.dll")]publicstaticexternintWNetCancelConnection2(stringsharename,intdwFlags,intfForce);publicstaticintConnect(stringremotePath,stringlocalPath,stringusername,stringpassword){NETRESOURCE[]share_driver=newNETRESOURCE[1];share_driver[0].dwScope=RESOURCE_SCOPE.RESOURCE_GLOBALNET;share_driver[0].dwType=RESOURCE_TYPE.RESOURCETYPE_DISK;share_driver[0].dwDisplayType=RESOURCE_DISPLAYTYPE.RESOURCEDISPLAYTYPE_SHARE;share_driver[0].dwUsage=RESOURCE_USAGE.RESOURCEUSAGE_CONNECTABLE;share_driver[0].lpLocalName=localPath;share_driver[0].lpRemoteName=remotePath;Disconnect(localPath);intret=WNetAddConnection2(share_driver,password,username,1);returnret;}publicstaticintDisconnect(stringlocalpath){returnWNetCancelConnection2(localpath,1,1);}}}
我用这段,可以映射成功