求:访问共享文件夹的方法(9x++ & NT++)UP有分

a11s 2003-11-08 04:21:03
加精
预防针1:不要给我说用Net Use 或者 Net Share
预防针2:要的不是共享方法,是访问方法
主体:
感谢大家的相助,总算知道了如何共享文件夹,但是我不会访问
举一个例子:我已经共享了sharename= \\10.0.0.1\sharedir 这个文件夹
我怎样才能 1、列出sharedir的内容(像windows一样,他好像没有影射驱动器)
(包括子文件夹,windoz连属性都能看)
2、复制sharedir下的文件到本地
3、怎样才能实现像windows那样的进度控制
我已经搜索过CSDN99年以后的帖子,没有找到满意答案。劳烦诸位N星了。
小生在此谢过。
E-Mail:dsonic@sohu.com dsonic@msn.com (最好两个都发,不要忘记写下您在CSDN上的昵称)
QQ:51496532 用户验证消息: CSDN
...全文
717 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
dreamreality 2004-02-02
  • 打赏
  • 举报
回复
up
rainstormmaster 2003-11-09
  • 打赏
  • 举报
回复
//问题基本上解决了
且慢,在98下试过了吗,在98下更改共享文件夹的密码和访问类型,用zqfleaf(动力港湾)的代码可是不行的 :)

//可不可以不用FSO。应该有其他的办法吧
当然可以了,等会我写点代码贴上来

a11s 2003-11-09
  • 打赏
  • 举报
回复
问题基本上解决了。还有一点点的遗憾
可不可以不用FSO,对于引用来说不一定每个操作系统都有
而且不是每个人都喜欢FSO的(当然咱们例外,毕竟方便嘛)
对于win98来说,这项功能在个人用户面前几乎没用,而且容易感染蠕虫
所以有很多文章建议卸载这个组件。这样我的程序在通用性上就不是那么很好了。

可不可以不用FSO。应该有其他的办法吧。
(sorry rainstormmaster不是对您有意见,您的办法很好,我只是想知道有没有更完美的解决办法而已)

===========================
如果这个问题解决了,那么这个帖子可以组成局域网共享的完美解决方案了。
建议斑竹加入精华贴
a11s 2003-11-09
  • 打赏
  • 举报
回复
谢谢zqfleaf(动力港湾)
一般我使用Net Use 影射网络驱动器,至于这种不需要盘符的Net use 没有用过
=========20分钟之后============
明白了, 使用net use \\10.0.0.3\movie password /user:username
之后Windows会“记住”刚才输入的密码,以再次访问时不会提示输入密码
这样可以实现针对密码的访问
========之所以不想用Net Use的原因=========
因为win9x 的Net命令功能不全,不像win2000
而且针对DOS命令(姑且让我这么叫吧)要求高,万一Net改名就没法用了

再次感谢 zqfleaf(动力港湾)
zqfleaf 2003-11-09
  • 打赏
  • 举报
回复
用net use 可以,只是你不想用shell命令是不是?
net use 是可以在windows中建立一个联接(你可以用net use可以看到所有的联接)
你现在是不是不想用net use 而用api来代替?
================
把下面代码放到module中
Option Explicit
Const WN_Success = &H0
Const WN_Not_Supported = &H1
Const WN_Net_Error = &H2
Const WN_Bad_Pointer = &H4
Const WN_Bad_NetName = &H32
Const WN_Bad_Password = &H6
Const WN_Bad_Localname = &H33
Const WN_Access_Denied = &H7
Const WN_Out_Of_Memory = &HB
Const WN_Already_Connected = &H34
Public ErrorNum As Long
Public ErrorMsg As String
Public rc As Long
Private Const ERROR_NO_CONNECTION = 8
Private Const ERROR_NO_DISCONNECT = 9
Private 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
Private 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
Const NO_ERROR = 0
Const CONNECT_UPDATE_PROFILE = &H1
Const RESOURCETYPE_DISK = &H1
Const RESOURCETYPE_PRINT = &H2
Const RESOURCETYPE_ANY = &H0
Const RESOURCE_GLOBALNET = &H2
Const RESOURCEDISPLAYTYPE_SHARE = &H3
Const RESOURCEUSAGE_CONNECTABLE = &H1

Public Function ConnectUserPassword(sDrive As String, sService As String, Optional sUser As String = "", Optional sPassword As String = "") As Boolean
Dim NETR As NETRESOURCE
Dim errInfo As Long
With NETR
.dwScope = RESOURCE_GLOBALNET
.dwType = RESOURCETYPE_DISK
.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
.dwUsage = RESOURCEUSAGE_CONNECTABLE
.lpRemoteName = sDrive
.lpLocalName = sService
End With
errInfo = WNetAddConnection2(NETR, sPassword, sUser, CONNECT_UPDATE_PROFILE)
ConnectUserPassword = errInfo = NO_ERROR
End Function


================
call ConnectUserPassword("\\servername", "", "password", "username")

相当于 net use \\servername password /user:username
不知道是否可以?返回值代表成功或失败
a11s 2003-11-09
  • 打赏
  • 举报
回复
To rainstormmaster
您的意思我明白,让我重新设置服务器的管理权限。
但是管理员绝对不会允许我这么做的。
a11s 2003-11-09
  • 打赏
  • 举报
回复
这是我引用别人的模块,(对不起,原作者的名字不是我删的,bas拿来时已经没有了)。
请大家不要再怀疑设置共享方面还有问题,主要是如何才能访问带密码的共享
Option Explicit

'2000 下用API来实现目录共享及删除共享

'共享类型
Private Const STYPE_ALL As Long = -1
Private Const STYPE_DISKTREE As Long = 0
Private Const STYPE_PRINTQ As Long = 1
Private Const STYPE_DEVICE As Long = 2
Private Const STYPE_IPC As Long = 3
Private Const STYPE_SPECIAL As Long = &H80000000

'权限
Private Const ACCESS_READ As Long = &H1
Private Const ACCESS_WRITE As Long = &H2
Private Const ACCESS_CREATE As Long = &H4
Private Const ACCESS_EXEC As Long = &H8
Private Const ACCESS_DELETE As Long = &H10
Private Const ACCESS_ATRIB As Long = &H20
Private Const ACCESS_PERM As Long = &H40
Private Const ACCESS_ALL As Long = ACCESS_READ Or _
ACCESS_WRITE Or _
ACCESS_CREATE Or _
ACCESS_EXEC Or _
ACCESS_DELETE Or _
ACCESS_ATRIB Or _
ACCESS_PERM

'共享信息
Private Type SHARE_INFO_2
shi2_netname As Long '共享名
shi2_type As Long '类型
shi2_remark As Long '备注
shi2_permissions As Long '权限
shi2_max_uses As Long '最大用户
shi2_current_uses As Long '
shi2_path As Long '路径
shi2_passwd As Long '密码
End Type

'设置共享
Private Declare Function NetShareAdd Lib "netapi32" _
(ByVal ServerName As Long, _
ByVal level As Long, _
buf As Any, _
parmerr As Long) As Long

'删除共享
Private Declare Function NetShareDel Lib "netapi32.dll" _
(ByVal ServerName As Long, _
ByVal ShareName As Long, _
ByVal dword As Long) As Long


'设置共享(返回0 为成功)
'参数:
'sServer 计算机名
'sSharePath 要共享路径
'sShareName 显示的共享名
'sShareRemark 备注
'sSharePw 密码
Private Function ShareAdd(sServer As String, _
sSharePath As String, _
sShareName As String, _
sShareRemark As String, _
sSharePw As String) As Long

Dim lngServer As Long
Dim lngNetname As Long
Dim lngPath As Long
Dim lngRemark As Long
Dim lngPw As Long
Dim parmerr As Long
Dim si2 As SHARE_INFO_2

lngServer = StrPtr(sServer) '转成地址
lngNetname = StrPtr(sShareName)
lngPath = StrPtr(sSharePath)

'如果有备注信息
If Len(sShareRemark) > 0 Then
lngRemark = StrPtr(sShareRemark)
End If

'如果有密码
If Len(sSharePw) > 0 Then
lngPw = StrPtr(sSharePw)
End If

'初始化共享信息
With si2
.shi2_netname = lngNetname
.shi2_path = lngPath
.shi2_remark = lngRemark
.shi2_type = STYPE_DISKTREE
.shi2_permissions = ACCESS_ALL
.shi2_max_uses = -1
.shi2_passwd = lngPw
End With

'设置共享(用户名,共享类型,共享信息,)
ShareAdd = NetShareAdd(lngServer, _
2, _
si2, _
parmerr)

End Function

'删除共享(返回0 为成功)
'参数:
'sServer 计算机名
'sShareName 共享名
Private Function DelShare(sServer As String, _
sShareName As String) As Long

Dim lngServer As Long '计算机名
Dim lngNetname As Long '共享名

lngServer = StrPtr(sServer) '转成地址
lngNetname = StrPtr(sShareName)

'删除共享
DelShare = NetShareDel(lngServer, lngNetname, 0)

End Function
(感谢源代码提供者)
rainstormmaster 2003-11-09
  • 打赏
  • 举报
回复
我是要你设置“完全”权限后,进行访问
a11s 2003-11-09
  • 打赏
  • 举报
回复
首先感谢rainstormmaster的大力支持!
第一个连接有一个跳转,转到用VBS如何批量设置权限(不是访问)
第二、三个连接是如何使用NetShareAdd、NetShareDel Lib "netapi32.dll" 设置权限(不是访问)
最后一个连接是如何使用Net Share

似乎都不能解决远程登陆时的用户名密码问题
rainstormmaster 2003-11-09
  • 打赏
  • 举报
回复
呵呵,我是说的不够严谨,其实我所指的只是OPEN、kill。

关于共享的权限和密码问题,可以通过api搞定,参考:
http://expert.csdn.net/Expert/topic/2383/2383823.xml?temp=.4354364
http://expert.csdn.net/Expert/topic/2437/2437228.xml?temp=.1366693
http://expert.csdn.net/Expert/topic/2371/2371234.xml?temp=.311352
http://expert.csdn.net/Expert/topic/2371/2371069.xml?temp=.9437525
a11s 2003-11-09
  • 打赏
  • 举报
回复
现在可以说问题已经解决了
虽然还有一些小小的问题,比如列出文件列表时有时会溢出。这个我会自己想办法解决的
本来只想问一下如何连接的谁知道热心的rainstormmaster连98下的共享都列出来了。再次感谢rainstormmaster的帮助。
总结:
其实回过头来看看,这个问题的答案其实并不困难。是我想的太复杂了。没想到简单的FindFirstFile、FindNextFile解决了我的问题。还有远程文件的访问居然可以直接Open...
真是应了这句话:“没有做不到,只有想不到”

还有感谢 动力港湾 提供的mpr.dll的使用方法。原来只是映射网络驱动器,没有想到不用映射也可以建立连接。又是一个“没想到”。建议大家以后遇到问题时多多尝试一下。说不定就成功了呢 ^o^

还有感谢帮忙UP的热心人。
最后祝愿大家事业有成!
=======================================================
(如果没有什么补充我就要结帖了)
a11s 2003-11-09
  • 打赏
  • 举报
回复
'经过我的测试,两种方法都可以实现。现在把它整理出来
'=============================================
Option Explicit
Private Const RESOURCETYPE_DISK = &H1
Private Const RESOURCETYPE_ANY = &H0
Private Const RESOURCETYPE_PRINT = &H2
Private Const RESOURCETYPE_UNKNOWN = &HFFFF
Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long, ByVal fForce As Long) As Long

Private Declare Function WNetDisconnectDialog Lib "mpr.dll" (ByVal hWnd As Long, ByVal dwType As Long) As Long

Public Function DisconnectNetworkDialog() As Long
DisconnectNetworkDialog = WNetDisconnectDialog(0&, RESOURCETYPE_DISK)
End Function


Private Sub Command1_Click() '第一种方法
Call DisconnectNetworkDialog
End Sub

Private Sub Command2_Click()' 第二种方法
Call WNetCancelConnection2("\\10.0.0.1\temp", 1, True)
End Sub
taosihai1only 2003-11-09
  • 打赏
  • 举报
回复
好,关注中.....
zqfleaf 2003-11-09
  • 打赏
  • 举报
回复
不行试试这个:
Public Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long, ByVal fForce As Long) As Long


然后:
call WNetCancelConnection2("\\servername",1,true)
zqfleaf 2003-11-09
  • 打赏
  • 举报
回复
加上定义:
Private Declare Function WNetDisconnectDialog Lib "mpr.dll" (ByVal hWnd As Long, ByVal dwType As Long) As Long

Public Function DisconnectNetworkDialog() As Long
DisconnectNetworkDialog = WNetDisconnectDialog(0&, RESOURCETYPE_DISK)
End Function

=========
在程序中call DisconnectNetworkDialog()
会弹出一个联接对话框,选择哪一个断开,就可以断开了。
a11s 2003-11-09
  • 打赏
  • 举报
回复
可以查看目录了
谢谢rainstormmaster
================================
总体解决办法是这样的:
1 根据操作系统决定使用哪一个DLL建立共享
2 利用 动力港湾 提出的的mpr.dll建立连接(经检查winMe有,win98就不知道了)
3 按照rainstormmaster提出的方法进行列表
4 然后open.....get.. put ..close
5 (好像少了关闭网络连接)(好像也没听说过)(但是Net use 有/delete的)
================================
之差最后一步了,完成之后就可以结帖了
小生能够得到诸位大哥的帮助深感荣幸,在这里谢谢大家了。
WilliamXiaoLiang 2003-11-09
  • 打赏
  • 举报
回复
up!
a11s 2003-11-09
  • 打赏
  • 举报
回复
我的Win98开共享方法基本上跟您的是一样的
我先试试FindFirstFile...
这几个API平时用的不多。看来一般也就这个时候用了
=======正在尝试===========
rainstormmaster 2003-11-09
  • 打赏
  • 举报
回复
//可不可以不用FSO。应该有其他的办法吧
当然可以了,等会我写点代码贴上来

就用现成的例子吧:
'Create a form with a command button (command1), a list box (list1)
'and four text boxes (text1, text2, text3 and text4).
'Type in the first textbox a startingpath like c:\(这里你输入共享文件夹的路径,如:
\\ns\mytest试试)
'and in the second textbox you put a pattern like *.* or *.txt

Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long

Const MAX_PATH = 260
Const MAXDWORD = &HFFFF
Const INVALID_HANDLE_VALUE = -1
Const FILE_ATTRIBUTE_ARCHIVE = &H20
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Const FILE_ATTRIBUTE_HIDDEN = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_ATTRIBUTE_READONLY = &H1
Const FILE_ATTRIBUTE_SYSTEM = &H4
Const FILE_ATTRIBUTE_TEMPORARY = &H100

Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Function StripNulls(OriginalStr As String) As String
If (InStr(OriginalStr, Chr(0)) > 0) Then
OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
End If
StripNulls = OriginalStr
End Function

Function FindFilesAPI(path As String, SearchStr As String, FileCount As Integer, DirCount As Integer)
'KPD-Team 1999
'E-Mail: KPDTeam@Allapi.net

Dim FileName As String ' Walking filename variable...
Dim DirName As String ' SubDirectory Name
Dim dirNames() As String ' Buffer for directory name entries
Dim nDir As Integer ' Number of directories in this path
Dim I As Integer ' For-loop counter...
Dim hSearch As Long ' Search Handle
Dim WFD As WIN32_FIND_DATA
Dim Cont As Integer
If Right(path, 1) <> "\" Then path = path & "\"
' Search for subdirectories.
nDir = 0
ReDim dirNames(nDir)
Cont = True
hSearch = FindFirstFile(path & "*", WFD)
If hSearch <> INVALID_HANDLE_VALUE Then
Do While Cont
DirName = StripNulls(WFD.cFileName)
' Ignore the current and encompassing directories.
If (DirName <> ".") And (DirName <> "..") Then
' Check for directory with bitwise comparison.
If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
dirNames(nDir) = DirName
DirCount = DirCount + 1
nDir = nDir + 1
ReDim Preserve dirNames(nDir)
End If
End If
Cont = FindNextFile(hSearch, WFD) 'Get next subdirectory.
Loop
Cont = FindClose(hSearch)
End If
' Walk through this directory and sum file sizes.
hSearch = FindFirstFile(path & SearchStr, WFD)
Cont = True
If hSearch <> INVALID_HANDLE_VALUE Then
While Cont
FileName = StripNulls(WFD.cFileName)
If (FileName <> ".") And (FileName <> "..") Then
FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
FileCount = FileCount + 1
List1.AddItem path & FileName
End If
Cont = FindNextFile(hSearch, WFD) ' Get next file
Wend
Cont = FindClose(hSearch)
End If
' If there are sub-directories...
If nDir > 0 Then
' Recursively walk into them...
For I = 0 To nDir - 1
FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(I) & "\", SearchStr, FileCount, DirCount)
Next I
End If
End Function
Sub Command1_Click()
Dim SearchPath As String, FindStr As String
Dim FileSize As Long
Dim NumFiles As Integer, NumDirs As Integer
Screen.MousePointer = vbHourglass
List1.Clear
List1.Visible = False
SearchPath = Text1.Text
FindStr = Text2.Text
FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
Text3.Text = NumFiles & " Files found in " & NumDirs + 1 & " Directories"
Text4.Text = "Size of files found under " & SearchPath & " = " & Format(FileSize, "#,###,###,##0") & " Bytes"
Screen.MousePointer = vbDefault
List1.Visible = True
End Sub

xiaohuangtao 2003-11-09
  • 打赏
  • 举报
回复
最奸单的办法把:
将该文件夹加到FTP服务中啊,用Ftp访问嘛,哪里有这么复杂啊。
功能实现就OK!
我的软件下载地址:http://www.cnkernel.com/
加载更多回复(8)

1,502

社区成员

发帖
与我相关
我的任务
社区描述
VB 网络编程
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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