vb 获得选中文件夹或文件路径

作者&投稿:宜轻 (若有异议请与网页底部的电邮联系)
vb 如何通过一个文件的全路径获得该文件所在文件夹~

解决这个问题有两种方法。
第一种:知道了文件的全路径,那么路径中当然也包含文件所在的文件夹信息,只要从中提取即可。例如,已知文件全路径为“C:\Windows\System32\abc.dll”并赋予变量strPt,可用过下面语句获取文件夹。
left(strPt,instrrev(strPt,"\"))instrrev函数的作用是从右侧开始查找指定字符串,并返回数值,此处返回值为20.left函数的作用是从左往右取N个字符,此例中取20个,最后结果为:C:\Windows\System32\。
第二种方法:使用FileSystemObject对象。代码如下:
dim fso as object, strFolder as objectset fso = createobject("scripting.filesystemobject")set strFolder = fso.getfolder("C:\Windows\System32\abc.dll")msgbox strFolder.path文件系统对象FSO的英文全称是File System Object ,这种对象模型提出了有别于传统的文件操作语句处理文件和文件夹的方法。通过采用object.method这种在面向对象编程中广泛使用的语法,将一系列操作文件和文件夹的动作通过调用对象本身的属性直接实现。

Path = "c:\soft\中国人\setup.exe"
For i = 1 To Len(Path)
If Mid(Path, i, 1) = "\" Then Print Left(Path, i)
Next i
或者:
Path = "c:\soft\中国人\setup.exe"
For i = 1 To Len(Path)
If Mid(Path, i, 1) = "\" Then Text1.Text = Left(Path, i)
Next i
将路径存放于text1内

以下是我的代码,已经测试通过:

Dim fso As Object
Dim wsh As Object
Dim DesktopPath As String
Dim bReady As Boolean

Public Function formatPath(sPath As String) As String
formatPath = sPath
If Right(sPath, 1) <> "\" Then formatPath = sPath & "\"
End Function

Private Sub Form_Load()
Set fso = CreateObject("Scripting.FileSystemObject")
Set wsh = CreateObject("WScript.Shell")
DesktopPath = wsh.SpecialFolders("Desktop") & "\" '获取桌面路径

bReady = False
End Sub

Private Sub Form_Unload(Cancel As Integer)
Set fso = Nothing
Set wsh = Nothing
End Sub

Private Sub Dir1_Change()
File1.Path = Dir1.Path ' 当目录改变时,设置文件路径.
End Sub

Private Sub File1_Click()
sPath = formatPath(Dir1.List(Dir1.ListIndex)) '获取选择的路径

Text1.Text = sPath & File1.FileName

bReady = True
End Sub

Private Sub Command1_Click() '复制
If bReady = True Then fso.CopyFile Text1.Text, DesktopPath & File1.FileName
End Sub

Private Sub Command2_Click() '删除
If bReady = True Then fso.DeleteFile Text1.Text: File1.Refresh
End Sub

Private Sub Command3_Click() '剪切
If bReady = True Then fso.MoveFile Text1.Text, DesktopPath & File1.FileName: File1.Refresh
End Sub

1.
Private Sub Dir1_Change()
File1.Path = Dir1.Path
Text1 = Dir1.Path
End Sub

Private Sub Dir1_Click()
Dir1.Path = Dir1.List(Dir1.ListIndex)
End Sub

Private Sub File1_Click()
Text1 = Replace(Dir1.Path & "\", "\\", "\") & File1.FileName
End Sub


河间市17523456091: vb 请问如何获得鼠标当前选中的文件 -
任沿三七: 获取activewindow的句柄,属性等方法,可以搜索下 Private Declare Function GetActiveWindow Lib "user32" Alias "GetActiveWindow" () As Long Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

河间市17523456091: vb 获得选中文件夹或文件路径 -
任沿三七: 以下是我的代码,已经测试通过:Dim fso As Object Dim wsh As Object Dim DesktopPath As String Dim bReady As BooleanPublic Function formatPath(sPath As String) As StringformatPath = sPathIf Right(sPath, 1) <> "\" Then formatPath...

河间市17523456091: VB查找指定文件夹并获取文件路径的代码 -
任沿三七: 新建一个exe工程 添加Text1,Text2,Command1,List1 粘贴下面代码就能运行使用了 Dim kz As Boolean Private Function SearchFiles(Path As String, FileType As String) If kz Then Exit Function Dim Files() As String '文件路径 Dim Folder() As String...

河间市17523456091: VB获取文件路径 -
任沿三七: 先设置文本框的OLEDropMode = 1 然后参考以下代码: Private Sub Label2_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single)Label2.Caption = Data.Files(1) End Sub

河间市17523456091: vb中如何获得选中文件的地址并能使用 -
任沿三七: 就一度句 Private Sub Dir1_Change() Print Dir1.Path End Sub Private Sub Drive1_Change() Dir1.Path = Drive1.Drive End Sub

河间市17523456091: vb 获取文件夹路径的控件 -
任沿三七: 常用控件工具箱中的drive1和dir1 Private Sub Drive1_Change() Dir1.Path = Drive1.Drive End Sub Private Sub Dir1_Change() File1.Path = Dir1.Path Text1 = Dir1.Path '只要用户改变了文件夹,用文本框显示了文件夹的路径 End Sub Private Sub Drive1_Change() Dir1.Path = Drive1.Drive End Sub

河间市17523456091: vb 完整路径获取文件夹路径 -
任沿三七: Path = "c:\soft\中国人\setup.exe" For i = 1 To Len(Path) If Mid(Path, i, 1) = "\" Then Print Left(Path, i) Next i 或者: Path = "c:\soft\中国人\setup.exe" For i = 1 To Len(Path) If Mid(Path, i, 1) = "\" Then Text1.Text = Left(Path, i) Next i 将路径存放于text1内

河间市17523456091: VB 中如何获取文件路径???求救啊 -
任沿三七: private sub command1_click() CommonDialog1.ShowOpen print CommonDialog1.FileName end sub

河间市17523456091: vb查找文件并获取文件路径 -
任沿三七: Dir 函数示例:Dim MyFile, MyPath, MyName' 返回“WIN.INI” (如果该文件存在). MyFile = Dir("C:\WINDOWS\WIN.ini")' 返回带指定扩展名的文件名.如果超过一个 *.ini 文件存在, ' 函数将返回按条件第一个找到的文件名. MyFile = ...

河间市17523456091: vb 如何通过一个文件的全路径获得该文件所在文件夹 -
任沿三七: '编写函数,获取 Private Function GetLuJIn(ByVal a As String) As String Dim i As Long For i = Len(a) To 1 Step -1 If mid(a,i,1) = "\" Then '获取最后一个"\"位置 GetLuJin = Left(a,i-1) '函数返回最后一个"\"前面的东西 Exit Function '获取之后,函数没什么事了,退出 End if Next i End Function

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网