问题描述
在C#的(Winfrom或WEB)中如何去連接網絡上共享的目錄呢?
解决方案
解决方案二:
oo
解决方案三:
将网络路径映射为本地的一个虚拟盘然后就按操作本机文件一样操作这个文件就可以了
解决方案四:
网络上共享的目录如果权限比较低。在winform是里可以直接当作本地盘去操作的。如果有设有账号密码。则在代码里运行cmd执行netuse命令。把用户名密码作为参数打通路径之后再操作。b/s里无法访问共享。因为结构不一样。客户在ie访问的时候根本就没有这个环境。你只能把服务器的局域网环境里的文件取过来放在iis目录下。或用网址访问
解决方案五:
我在C#下實現,有代碼沒
解决方案六:
使用win32api[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].dwType=RESOURCE_TYPE.RESOURCETYPE_DISK;share_driver[0].lpLocalName=localPath;share_driver[0].lpRemoteName=remotePath;Disconnect(localPath);intret=WNetAddConnection2A(share_driver,password,username,1);returnret;}publicstaticvoidDisconnect(stringlocalpath){WNetCancelConnection2A(localpath,1,1);}
解决方案七:
C#里面调用cmd去执行netuse****的命令建立IPC连接。然后可以映射成为一个本地盘符。这样就可以像操作本地盘一样去操作远程映射目录了。
解决方案八:
publicclassWebForm2:System.Web.UI.Page{protectedSystem.Web.UI.WebControls.ButtonButton1;[DllImport("mpr.dll",EntryPoint="WNetAddConnection2")]privatestaticexternuintWNetAddConnection2([In]NETRESOURCElpNetResource,stringlpPassword,stringlpUsername,uintdwFlags);[DllImport("Mpr.dll")]privatestaticexternuintWNetCancelConnection2(stringlpName,uintdwFlags,boolfForce);[StructLayout(LayoutKind.Sequential)]privateclassNETRESOURCE{publicintdwScope;publicintdwType;publicintdwDisplayType;publicintdwUsage;publicstringLocalName;publicstringRemoteName;publicstringComment;publicstringProvider;}privatevoidPage_Load(objectsender,System.EventArgse){//在這裡放置使用者程式碼以初始化網頁}#regionWebForm設計工具產生的程式碼overrideprotectedvoidOnInit(EventArgse){////CODEGEN:此為ASP.NETWebForm設計工具所需的呼叫。//InitializeComponent();base.OnInit(e);}///<summary>///此為設計工具支援所必須的方法-請勿使用程式碼編輯器修改///這個方法的內容。///</summary>privatevoidInitializeComponent(){this.Button1.Click+=newSystem.EventHandler(this.Button1_Click);this.Load+=newSystem.EventHandler(this.Page_Load);}#endregionprivatevoidButton1_Click(objectsender,System.EventArgse){NETRESOURCEmyNetResource=newNETRESOURCE();myNetResource.dwScope=2;//2:RESOURCE_GLOBALNETmyNetResource.dwType=1;//1:RESOURCETYPE_ANYmyNetResource.dwDisplayType=3;//3:RESOURCEDISPLAYTYPE_GENERICmyNetResource.dwUsage=1;//1:RESOURCEUSAGE_CONNECTABLEmyNetResource.LocalName="Y:";myNetResource.RemoteName=@"\ftptemp";myNetResource.Provider=null;uintnret=WNetAddConnection2(myNetResource,"user","pwd",0);if(nret==0){Response.Write("0");}}為什麼我這樣還是不能將Y:映射出來啊
解决方案:
使用NETUSE来保存用户和密码.NET下就可以直接访问了http://blog.csdn.net/zgke/archive/2008/12/05/3451599.aspx
解决方案:
引用6楼kugou123的回复:
C#里面调用cmd去执行netuse****的命令建立IPC连接。然后可以映射成为一个本地盘符。这样就可以像操作本地盘一样去操作远程映射目录了。
netuse不是我想要的
解决方案:
有誰看一下我的代碼,是哪裡有問題啊?
解决方案:
引用8楼zgke的回复:
使用NETUSE来保存用户和密码.NET下就可以直接访问了http://blog.csdn.net/zgke/archive/2008/12/05/3451599.aspx
你這個還是用netuse,我知道調用Process是可以實現批處理的功能啦,但是我不想用這個WNetaddConnection2怎麼實現
解决方案:
引用7楼david0620的回复:
publicclassWebForm2:System.Web.UI.Page{protectedSystem.Web.UI.WebControls.ButtonButton1;[DllImport("mpr.dll",EntryPoint="WNetAddConnection2")]privatestaticexternuintWNetAddConnection2([In]NETRESOURCElpNetResource,stringlpPassword,stringlpUsername,uintdwFlags);[DllImport("Mpr.dll")]privatestaticexternuintWNetCancelConnection2(stringlpName,uintdwFl…
我在5楼贴的[DllImport("mpr.dll")]publicstaticexternintWNetAddConnection2A(NETRESOURCE[]lpNetResource,stringlpPassword,stringlpUserName,intdwFlags);调用时需要传入的是一个数组类型,你把传入类型给改了。我本地又试了一下可以映射上的。NETRESOURCE[]share_driver=newNETRESOURCE[1];share_driver[0].dwType=RESOURCE_TYPE.RESOURCETYPE_DISK;share_driver[0].lpLocalName=localPath;share_driver[0].lpRemoteName=remotePath;intret=WNetAddConnection2A(share_driver,password,username,1);
解决方案:
引用12楼flyjimi的回复:
引用7楼david0620的回复:publicclassWebForm2:System.Web.UI.Page{protectedSystem.Web.UI.WebControls.ButtonButton1;[DllImport("mpr.dll",EntryPoint="WNetAddConnection2")]privatestaticexternuintWNetAddConnection2([In]NETRESOURCElpNetResource,stringlpPassword,stringlpUsername,uintdwFlags);[DllImport("Mpr.dll")]privatestaticexternuintWNetCancel…
怎麼我用你的代碼還是不行啊?你看看我的代碼看看哪裡有問題?指點一下..
解决方案:
up
解决方案:
引用12楼flyjimi的回复:
引用7楼david0620的回复:publicclassWebForm2:System.Web.UI.Page{protectedSystem.Web.UI.WebControls.ButtonButton1;[DllImport("mpr.dll",EntryPoint="WNetAddConnection2")]privatestaticexternuintWNetAddConnection2([In]NETRESOURCElpNetResource,stringlpPassword,stringlpUsername,uintdwFlags);[DllImport("Mpr.dll")]privatestaticexternuintWNetCancel…
用你的代碼報錯:[DllImport("mpr.dll")]publicstaticexternintWNetAddConnection2A(NETRESOURCE[]lpNetResource,stringlpPassword,stringlpUserName,intdwFlags);[DllImport("mpr.dll")]publicstaticexternintWNetCancelConnection2A(stringsharename,intdwFlags,intfForce);NETRESOURCE[]share_driver=newNETRESOURCE[1];share_driver[0].dwType=RESOURCE_TYPE.RESOURCETYPE_DISK;share_driver[0].lpLocalName=localPath;share_driver[0].lpRemoteName=remotePath;Disconnect(localPath);intret=WNetAddConnection2A(@"serveriptemp","pwd","user",1);returnret;
解决方案:
報說沒有usingNETRESOURCE
解决方案:
做个记号
解决方案:
usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Runtime.InteropServices;namespaceWindowsApplication1{#regionWindowsAPIenumandstructpublicenumERROR_ID{ERROR_SUCCESS=0,//SuccessERROR_MORE_DATA=234,ERROR_NO_BROWSER_SERVERS_FOUND=6118,ERROR_INVALID_LEVEL=124,ERROR_ACCESS_DENIED=5,ERROR_INVALID_PARAMETER=87,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}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;}#endregion/**////<summary>///连接网络上的共享文件夹///</summary>publicclassNetworkSharedFolder{[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].dwType=RESOURCE_TYPE.RESOURCETYPE_DISK;share_driver[0].lpLocalName=localPath;share_driver[0].lpRemoteName=remotePath;Disconnect(localPath);intret=WNetAddConnection2A(share_driver,password,username,1);returnret;}publicstaticvoidDisconnect(stringlocalpath){WNetCancelConnection2A(localpath,1,1);}publicstaticvoidTest(){intret=NetworkSharedFolder.Connect(@"\192.168.1.2log",@"Y:","user1","user1");System.Console.WriteLine("connectret="+ret);}}}