局域网内,如何实现服务器A对服务器B的共享文件的访问?(注意:登录服务器B需要用户名和密码)急!!给高分!

chantree 2005-02-28 08:54:47
1)环境: 局域网内,有两服务器: A和B.
2)要求: 服务器B有个共享文件夹; 服务器A有个程序,要从服务器B的共享文件夹中读文件.
3)问题: 服务器A从服务器B的共享文件夹中读取文件之前,首先要登录服务器B,而访问服务器B需要用户名和密码. 请问用VB.NET如何实现?
...全文
537 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
jenet 2005-03-09
  • 打赏
  • 举报
回复
学习
华芸智森 2005-03-07
  • 打赏
  • 举报
回复
设定/取消 网络磁盘
来源:cww

使用方法:

建立连线
Function AddConnection(ByVal RemoteLocation As String, _
ByVal LocalDriver As String, _
ByVal Passwd As String, _
ByVal UserName As String) As Boolean
RemoteLocation : 为网络磁盘原始来源,格式为
\\RemoteComputer\ShareDirectory
LocalDriver : 对应本机的磁盘代号,如: "H:"
Passwd : 存取网络磁盘的Password,传Null表示不用密码
UserName : 存取网络磁盘的使用者代号
成功时传回True,否则为False

例:
Call AddConnection("\\Shih\cmias", "x:", vbNullString, "cww")

取消连线
Function CancelConnection(ByVal LocalDriver As String, _
ByVal ForceClose As Boolean) As Boolean
LocalDriver : 对应本机的磁盘代号,如: "H:"
ForceClose : True表示强迫结束连线,而不管有没有程式正与之连线
成功时传回True,否则为False
例:
Call CancelConnection("x:", True)

以下在.bas
Option Explicit

Type NETRESOURCE
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
lpLocalName As String
lpRemoteName As String
lpComment As String
lpProvider As String
End Type
Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Long) As Long
Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long, ByVal fForce As Long) As Long
Declare Function WNetGetLastError Lib "mpr.dll" Alias "WNetGetLastErrorA" (lpError As Long, ByVal lpErrorBuf As String, ByVal nErrorBufSize As Long, ByVal lpNameBuf As String, ByVal nNameBufSize As Long) As Long

Public Const RESOURCE_PUBLICNET = &H2
Public Const RESOURCETYPE_ANY = &H0
Public Const RESOURCEDISPLAYTYPE_GENERIC = &H0
Public Const RESOURCEUSAGE_CONNECTABLE = &H1
Public Const CONNECT_UPDATE_PROFILE = &H1
Public Function AddConnection(ByVal RemoteLocation As String, ByVal LocalDriver As String, _
ByVal Passwd As String, ByVal UserName As String) As Boolean
Dim ne As NETRESOURCE, i As Long
Dim errstr As String, errpriv As String, erno As Long

ne.dwDisplayType = RESOURCEDISPLAYTYPE_GENERIC
ne.dwScope = RESOURCE_PUBLICNET
ne.dwType = RESOURCETYPE_ANY
ne.dwUsage = RESOURCEUSAGE_CONNECTABLE
ne.lpComment = vbNullString
ne.lpLocalName = LocalDriver
ne.lpProvider = vbNullString
ne.lpRemoteName = RemoteLocation

i = WNetAddConnection2(ne, Passwd, UserName, 0)
If i = 0 Then
AddConnection = True
Else
AddConnection = False
errstr = String(256, 0)
errpriv = String(256, 0)
i = WNetGetLastError(erno, errstr, 256, errpriv, 256)
errstr = Left(errstr, InStr(1, errstr, Chr(0)) - 1)
MsgBox errstr, vbCritical
End If
End Function

Public Function CancelConnection(ByVal LocalDriver As String, ByVal ForceClose As Boolean) As Boolean
Dim i As Long
Dim errstr As String, errpriv As String, erno As Long

i = WNetCancelConnection2(LocalDriver, 0, IIf(ForceClose, 1, 0))
If i = 0 Then
CancelConnection = True
Else
CancelConnection = False
errstr = String(256, 0)
errpriv = String(256, 0)
i = WNetGetLastError(erno, errstr, 256, errpriv, 256)
errstr = Left(errstr, InStr(1, errstr, Chr(0)) - 1)
MsgBox errstr, vbCritical
End If

End Function




chantree 2005-03-07
  • 打赏
  • 举报
回复
楼上"shalen520(Love will keep us alive)"的方案:采用命令行方式,优点是简单,缺点是速度慢.
调用API作映射共享不错,但是共享映射最好不要显示出来.
本人最终采用的方案是调用API,如下.
请大家指正,待大家看过之后,稍候两日,结本帖.谢谢!
'API声明:
Declare Function WNetAddConnection2 Lib "mpr.dll" _
Alias "WNetAddConnection2A" ( _
ByVal lpNetResource As NETRESOURCE(), _
ByVal lpPassword As String, _
ByVal lpUserName As String, _
ByVal dwFlags As Integer) As Integer

'结构定义:
Structure NETRESOURCE
Public dwScope As Integer
Public dwType As Integer
Public dwDisplayType As Integer
Public dwUsage As Integer
Public lpLocalName As String
Public lpRemoteName As String
Public lpComment As String
Public lpProvider As String
End Structure

'API调用:
Public Function ConnnectNet(ByVal strConnPath As String, ByVal strConnAccount As String, ByVal strConnPassword As String) As Boolean
Dim ne(1) As NETRESOURCE
Dim i As Integer
ne(0).lpRemoteName = strConnPath
i = WNetAddConnection2(ne, strConnPassword, strConnAccount, 0)
If i = 0 Then
MsgBox("成功!")
Else
MsgBox("失败!")
End If
End Function
mysterious 2005-03-01
  • 打赏
  • 举报
回复
学习
尚和空四 2005-03-01
  • 打赏
  • 举报
回复
作映射共享
shalen520 2005-03-01
  • 打赏
  • 举报
回复
System.Diagnostics.Process pr = System.Diagnostics.Process.Start("cmd", " /c net use " + "\"\\\\" + "192.168.0.2\"" + " /user:\"AAA\" 123");

pr.StartInfo.CreateNoWindow = true;

pr.WaitForExit();
leeyeefeng2004 2005-03-01
  • 打赏
  • 举报
回复
建议搂住做个映射不就可以了。
tihua 2005-03-01
  • 打赏
  • 举报
回复
ding
simon8181 2005-03-01
  • 打赏
  • 举报
回复
mark
landlordh 2005-02-28
  • 打赏
  • 举报
回复
WMI or API 都是可以的

刚才就回答了一个问题:
http://community.csdn.net/Expert/topic/3813/3813211.xml?temp=.4722864

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧