C#访问网络共享文件夹的方法
本文实例为大家分享了C#访问网络共享文件夹的具体代码,供大家参考,具体内容如下
usingSystem.Runtime.InteropServices; publicenumERROR_ID { ERROR_SUCCESS=0,//Success ERROR_BUSY=170, ERROR_MORE_DATA=234, ERROR_NO_BROWSER_SERVERS_FOUND=6118, ERROR_INVALID_LEVEL=124, ERROR_ACCESS_DENIED=5, ERROR_INVALID_PASSWORD=86, ERROR_INVALID_PARAMETER=87, ERROR_BAD_DEV_TYPE=66, ERROR_NOT_ENOUGH_MEMORY=8, ERROR_NETWORK_BUSY=54, ERROR_BAD_NETPATH=53, 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 } 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")] publicstaticexternintWNetAddConnection2A(NETRESOURCE[]lpNetResource,stringlpPassword,stringlpUserName,intdwFlags); [DllImport("mpr.dll")] publicstaticexternintWNetCancelConnection2A(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=WNetAddConnection2A(share_driver,password,username,1); returnret; } publicstaticintDisconnect(stringlocalpath) { returnWNetCancelConnection2A(localpath,1,1); } }
测试方法:
publicvoidTestNetWorkConnection() { stringlocalpath="X:"; //intstatus=NetworkConnection.Connect(@"//192.168.0.2/test",localpath,@"test","test"); intstatus=NetworkConnection.Connect("////192.168.0.2//test",localpath,@"test","test"); if(status==(int)ERROR_ID.ERROR_SUCCESS) { FileStreamfs=newFileStream(localpath+@"//123.txt",FileMode.OpenOrCreate); using(StreamWriterstream=newStreamWriter(fs)) { stream.WriteLine("你好呀,成功了"); stream.Flush(); stream.Close(); } fs.Close(); } else { Console.WriteLine(status); } NetworkConnection.Disconnect(localpath); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。