DWORDWNetAddConnection2( LPNETRESOURCE lpNetResource, LPCTSTRlpPassword, LPCTSTRlpUsername, DWORDdwFlags ); |
typedefstruct _NETRESOURCE { DWORDdwScope; DWORDdwType; DWORDdwDisplayType; DWORDdwUsage; LPTSTRlpLocalName; LPTSTRlpRemoteName; LPTSTRlpComment; LPTSTRlpProvider; }NETRESOURCE; |
DWORDWNetCancelConnection2( LPCTSTRlpName, DWORDdwFlags, BOOLfForce ); |
public staticclass WNetConnectionHelper { [DllImport("mpr.dll", EntryPoint = "WNetAddConnection2")] privatestatic extern uintWNetAddConnection2(NetResource lpNetResource,string lpPassword, string lpUsername, uint dwFlags); [DllImport("Mpr.dll", EntryPoint = "WNetCancelConnection2"))] privatestatic extern uintWNetCancelConnection2(stringlpName, uint dwFlags, bool fForce); [StructLayout(LayoutKind.Sequential)] publicclass NetResource { publicint dwScope; publicint dwType; publicint dwDisplayType; publicint dwUsage; publicstring lpLocalName; publicstring lpRemoteName; publicstring lpComment; publicstring lpProvider; } publicstatic uint WNetAddConnection(NetResourcenetResource,stringusername,stringpassword) { uint result =WNetAddConnection2(netResource, password, username,0); returnresult; } publicstatic uint WNetAddConnection(string username, string password, string remoteName, string localName) { NetResource netResource =new NetResource(); netResource.dwScope = 2; //RESOURCE_GLOBALNET netResource.dwType = 1; //RESOURCETYPE_ANY netResource.dwDisplayType = 3; //RESOURCEDISPLAYTYPE_GENERIC netResource.dwUsage = 1; //RESOURCEUSAGE_CONNECTABLE netResource.lpLocalName = localName; netResource.lpRemoteName =remoteName.TrimEnd('//'); //netResource.lpRemoteName =lpComment; //netResource.lpProvider = null; uint result =WNetAddConnection2(netResource, password, username,0); returnresult; } publicstatic uint WNetCancelConnection(string name,uint flags,bool force) { uint nret =WNetCancelConnection2(name, flags, force); returnnret; } } |
try { string result = WNetConnectionHelper.WNetAddConnection("TestAccout(用户名)","xxxxxx(密码)",@"\\192.168.1.10\test(共享地址)",null(或指定映射路径,如"y:")); string []files = Directory.GetFiles(@"\\192.168.1.10\test\fw"); File.Create(@"\\192.168.1.10\test\test.txt"); uint n =WNetConnectionHelper.WNetCancelConnection("//192.120.40.10//test(或"y:")",1, true); //取消映射 } catch (Exception ex) { //编写异常处理代码 } |
protected voidPage_Load(object sender,EventArgse) { uint r =WNetConnectionHelper.WNetAddConnection("192.120.40.10//username","password", @"//192.120.40.10/test","Z:"); string filename= @"//192.120.40.10/test/fw/1.doc"; ReadFile(filename); } 写文件到Web输出: public voidReadFile(stringfilename) { Response.Clear(); Response.Charset = "utf-8"; Response.Buffer = true; this.EnableViewState = false; Response.ContentType = "application/ms-word"; Response.ContentEncoding =System.Text.Encoding.UTF8; Response.AppendHeader("Content-Disposition", "inline;filename=1.doc"); try { Response.WriteFile(filename, true); } catch(Exceptionex) { Response.Write(ex.ToString()); } Response.Flush(); } |
本文中的有些功能描述可能不够具体或妥当,如果需要了解mpr.dll的更多功能还是建议看MSDN文档。对于WNetConnectionHelper的实现存在一些需要完善的地方,不过对于一般的应用基本可以满足,在实际使用时还需要考虑多线程的情况。另外,在有同名驱动器映射的情况下需要做一些判断,而且弄清楚SystemError Code的含义对代码的调试及正确执行有很大的帮助。This is some sampletext. You are usingFCKeditor.
联系客服