Ⅰ 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內
Ⅱ VB 瀏覽文件夾獲取文件夾路徑
這個是我用VB做的文件選擇器,仿Linux的資源瀏覽器!
Ⅲ vb獲取啟動文件夾路徑
Private Const CSIDL_Users_STARTMENU_a = &H7 '當前用戶\開始-程序-啟動
' 聲明API函數Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As LongPrivate Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As Long) As LongPrivate Function GetSpecialPath(CSIDL As Long) As String Dim s As Long Dim path As String Dim pidl As Long '根據指定的文件夾獲得pidl s = SHGetSpecialFolderLocation(Me.hWnd, CSIDL, pidl) If s = NOERROR Then ' 根據r的返回值判斷是否有錯誤發生,如果沒有錯誤就獲取文件夾路徑 path = Space$(512) s = SHGetPathFromIDList(ByVal pidl, ByVal path) GetSpecialPath = Left$(path, InStr(path, Chr$(0)) - 1) Exit Function End If GetSpecialPath = ""End Function
Private Sub Form_Load()
msgbox GetSpecialPath(CSIDL_Users_STARTMENU_a) & "\"
End Sub 其它參數Private Const NOERROR = 0Private Const CSIDL_Users_FAVORITES = &H6 '當前用戶\收藏夾Private Const CSIDL_Users_DESKTOPDIRECTORY = &H10 '當前用戶\桌面Private Const CSIDL_Users_STARTMENU = &HB '當前用戶\開始菜單Private Const CSIDL_Users_STARTMENU_cx = &H2 '當前用戶\開始-程序Private Const CSIDL_Users_MyDocuments = &H5 '當前用戶\我的文檔Private Const CSIDL_Users_STARTMENU_a = &H7 '當前用戶\開始-程序-啟動Private Const CSIDL_Users_Recent = &H8 '當前用戶\'RecentPrivate Const CSIDL_Users_SendTo = &H9 '當前用戶\SendToPrivate Const CSIDL_Users_MyMusic = &HD '當前用戶\My Documents\My Music\Private Const CSIDL_Users_NetHood = &H13 '當前用戶\NetHoodPrivate Const CSIDL_Users_Templates = &H15 '當前用戶\TemplatesPrivate Const CSIDL_Users_AppData = &H1A '當前用戶\Application Data\Private Const CSIDL_Users_PrintHood = &H1B '當前用戶\PrintHood\Private Const CSIDL_Users_Local_AppData = &H1C '當前用戶\Local Settings\Application Data\Private Const CSIDL_Users_Temp = &H20 '當前用戶\Local Settings\Temporary Internet Files\Private Const CSIDL_Users_Cookies = &H21 '當前用戶\Cookies\Private Const CSIDL_Users_History = &H22 '當前用戶\Local Settings\History\Private Const CSIDL_Users_Pictures = &H27 '當前用戶\My Documents\My Pictures\Private Const CSIDL_Users = &H28 '當前用戶\Private Const CSIDL_Users_gl = &H30 '當前用戶\「開始」菜單\程序\管理工具\Private Const CSIDL_Users_CDBurning = &H3B '當前用戶\Local Settings\Application Data\Microsoft\CD Burning\Private Const CSIDL_AllUsers_STARTMENU = &H16 'All Users\「開始」菜單\Private Const CSIDL_AllUsers_STARTMENU_cx = &H17 'All Users\「開始」菜單\程序\Private Const CSIDL_AllUsers_STARTMENU_j = &H18 'All Users\「開始」菜單\程序\啟動\Private Const CSIDL_AllUsers_DESKTOPDIRECTORY = &H19 'All Users\桌面Private Const CSIDL_AllUsers_FAVORITES = &H1F 'All Users\Favorites\(收藏夾)Private Const CSIDL_AllUsers_Templates = &H2D 'All Users\Templates\Private Const CSIDL_AllUsers_Documents = &H2E 'All Users\Documents\Private Const CSIDL_AllUsers_gl = &H2F 'All Users\「開始」菜單\程序\管理工具\Private Const CSIDL_AllUsers_Music = &H35 'All Users\Documents\My Music\Private Const CSIDL_AllUsers_Pictures = &H36 'All Users\Documents\My Pictures\Private Const CSIDL_AllUsers_Videos = &H37 'All Users\Documents\My Videos\Private Const CSIDL_AllUsers_AppData = &H23 'All Users\Application Data\Private Const CSIDL_WinDows = &H24 '系統安裝路徑 C:\WINDOWS\Private Const CSIDL_WinSystem = &H25 '系統文件夾 C:\WINDOWS\system32\Private Const CSIDL_ProgramFiles = &H26 '應用程序安裝文件夾 C:\Program Files\Private Const CSIDL__ProgramFiles_CommonFiles = &H2B 'C:\Program Files\Common Files\Private Const CSIDL_WIN_Resources = &H38 'C:\WINDOWS\Resources\Private Const CSIDL_font = &H14 '字體文件夾C:\WINDOWS\Fonts\
Ⅳ vb 得到文件路徑
「當用Dialog一次打開多個文件」————你這里的Dialog指的是通用的OpenDialog「打開」對話框嗎?
這樣做:
引用Common Dialog 控制項庫。
在窗口上添加一個List1,一個Label1,兩個按鈕Command1和Command2,一個ComDlg控制項命名為CD1。
然後加入以下代碼:
Option Explicit
Private Sub Command1_Click()
CD1.Flags = cdlOFNAllowMultiselect Or cdlOFNExplorer
CD1.ShowOpen
If CD1.FileName = "" Then Exit Sub
Dim Str1 As String
Dim Ary1() As String
Str1 = CD1.FileName
Ary1 = Split(Str1, Chr(0))
If UBound(Ary1) = 0 Then
ReDim Preserve Ary1(1)
Dim n1 As Long
n1 = InStrRev(Ary1(0), "\")
Ary1(1) = Mid(Ary1(0), n1 + 1)
Ary1(0) = Left(Ary1(0), n1)
End If
Dim s1 As Long, Apath1 As String
Apath1 = App.Path
If Right(Apath1, 1) <> "\" Then Apath1 = Apath1 & "\"
If Right(Ary1(0), 1) <> "\" Then Ary1(0) = Ary1(0) & "\"
Open Apath1 & "st.ini" For Output As #2
For s1 = 1 To UBound(Ary1)
Print #2, Ary1(0) & Ary1(s1)
Next
Close #2
End Sub
Private Sub Command2_Click()
Dim s1 As Long, Apath1 As String
Apath1 = App.Path
If Right(Apath1, 1) <> "\" Then Apath1 = Apath1 & "\"
Dim Str1 As String
Open Apath1 & "st.ini" For Input As #3
Do Until EOF(3)
Line Input #3, Str1
List1.AddItem Str1
Loop
Close #3
End Sub
Private Sub List1_Click()
If List1.ListIndex < 0 Then Exit Sub
Label1.Caption = List1.List(List1.ListIndex)
End Sub
ok,運行看看吧。
點擊Command1會打開文件並把文件路徑寫入st.ini
點擊Command2會讀取st.ini,並顯示到List1中
點擊List1中的項目會在Label中顯示內容。
完成了,已經在VB6下調試通過,給分吧~~
Ⅳ 如何用VB獲得某一目錄下的所有文件和文件夾名稱
Dim a() As String
Private Sub Dir1_Change()
File1.Path = Dir1.Path
Label1.Caption = Dir1.Path
End Sub
Private Sub Drive1_Change()
On Error GoTo err1
Dir1.Path = Drive1.Drive
Exit Sub
err1:
MsgBox Err.Description, vbInformation + vbOKOnly, "提示"
End Sub
Private Sub Command1_Click()
Dim i As Integer, j As Integer
Dim ifieldcount As Integer, irecordcount As Integer
Dim wdapp As Word.Application
Dim wddoc As Word.Document
Dim atable As Word.Table
If Option2.Value = True Then
ReDim a(1 To File1.ListCount)
For i = 1 To File1.ListCount
b = File1.List(i)
a(i) = b
Next
End If
If Option1.Value = True Then
ReDim a(1 To Dir1.ListCount)
For i = 1 To Dir1.ListCount
b = Dir1.List(i)
a(i) = b
Next
End If
End Sub
這段程序 復制過去 就行了 創建的控制項有 兩個option 讓你選擇 要提取文件夾 還是文件路徑的 選中option 1 就是提取文件夾的 option2 就是提取文件的 然後再創建 Drive1 Dir1 File1 這三個 選擇文件路徑的 控制項 再加一個 command 按鈕 就行了 有疑問再聯系我!!!!!這個 只是 文件路徑 你看看 是你想要的話 我再給你寫 提取文件夾和文件名的代碼!!!!
Ⅵ vb 如何通過一個文件的全路徑獲得該文件所在文件夾
'編寫函數,獲取
PrivateFunctionGetLuJIn(ByValaAsString)AsString
DimiAsLong
Fori=Len(a)To1Step-1
Ifmid(a,i,1)=""Then'獲取最後一個""位置
GetLuJin=Left(a,i-1)'函數返回最後一個""前面的東西
ExitFunction'獲取之後,函數沒專什麼事了,退出屬
Endif
Nexti
EndFunction
Ⅶ vb 如何通過一個文件的全路徑獲得該文件所在文件夾
dim
s
as
string
dim
i
s="d:\123\123\asd\123.txt"
i=InStrRev(s,"\")
''最右側
\
字元位置
s
=Left$(s,
i-1)
''s即是路徑(文件夾)d:\123\123\asd
debug.print
s
''顯示
d:\123\123\asd
Ⅷ VB獲取文件夾路徑
用API遍歷當前桌面已打開的子窗口,獲得窗口句柄和ID,然後通過ID獲得該文件夾的路徑,總之一句話,你要實現的功能VB無法做到,API可以
Ⅸ vb 獲得選中文件夾或文件路徑
以下是我的代碼,已經測試通過:
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
Ⅹ VB獲取文件路徑是怎麼樣的
VB2008測試通過 Imports System Imports System.IO Imports System.Threading Imports System.Windows.Formsx0dx0a Public Class FrmMainx0dx0ax0dx0aDim newSearcher As getFileByNamex0dx0ax0dx0a Private Sub FrmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loadx0dx0ax0dx0arball.Checked = Truex0dx0ax0dx0atbpath.Text = ""x0dx0ax0dx0atbpath.Enabled = Falsex0dx0ax0dx0abtndlg.Enabled = Falsex0dx0ax0dx0aEnd Subx0dx0ax0dx0a Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Clickx0dx0ax0dx0atv.Nodes.Clear()x0dx0ax0dx0abtnOK.Enabled = Falsex0dx0ax0dx0abtnExit.Enabled = Falsex0dx0ax0dx0aIf rball.Checked Thenx0dx0ax0dx0arun() tbpath.Text = "" Thenx0dx0ax0dx0aMsgBox("請輸入或者選擇一個路徑")x0dx0ax0dx0aExit (tbpath.Text)x0dx0ax0dx0aEnd Ifx0dx0ax0dx0aEnd Ifx0dx0ax0dx0aEnd Subx0dx0ax0dx0a Private Sub btndlg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndlg.Clickx0dx0ax0dx0afbdlg.RootFolder = Environment.SpecialFolder.MyComputerx0dx0ax0dx0afbdlg.ShowDialog()x0dx0ax0dx0atbpath.Text = fbdlg.SelectedPathx0dx0ax0dx0aEnd Subx0dx0ax0dx0a Private Sub rbchs_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbchs.CheckedChangedx0dx0ax0dx0aIf rbchs.Checked = True Thenx0dx0ax0dx0atbpath.Enabled = Truex0dx0ax0dx0abtndlg.Enabled = .Enabled = Falsex0dx0ax0dx0abtndlg.Enabled = Falsex0dx0ax0dx0aEnd Ifx0dx0ax0dx0aEnd Subx0dx0ax0dx0a Private Sub run()x0dx0ax0dx0aIf tbfileName.Text = "" Thenx0dx0ax0dx0aMsgBox("請輸入文件名稱")x0dx0ax0dx0abtnOK.Enabled = Truex0dx0ax0dx0abtnExit.Enabled = Truex0dx0ax0dx0aExit = New getFileByName(tbfileName.Text)x0dx0ax0dx0anewSearcher.GetDrivers()x0dx0ax0dx0atmr.Enabled = Truex0dx0ax0dx0atmr.Interval = 10x0dx0ax0dx0aEnd Ifx0dx0ax0dx0aEnd Subx0dx0ax0dx0a Private Sub run(ByVal path As String)x0dx0ax0dx0aIf tbfileName.Text = "" Thenx0dx0ax0dx0aMsgBox("請輸入文件名稱")x0dx0ax0dx0abtnOK.Enabled = Truex0dx0ax0dx0abtnExit.Enabled = Truex0dx0ax0dx0aExit = New getFileByName(tbfileName.Text)x0dx0ax0dx0anewSearcher.SearchFolder(path)x0dx0ax0dx0atmr.Enabled = Truex0dx0ax0dx0atmr.Interval = 50x0dx0ax0dx0aEnd Ifx0dx0ax0dx0aEnd Subx0dx0ax0dx0a Private Sub tmr_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr.Tickx0dx0ax0dx0aLblshow.Text = newSearcher.currentPathx0dx0ax0dx0aIf newSearcher.cntThread = 0 And newSearcher.isDone Thenx0dx0ax0dx0a'MsgBox(newSearcher.getFile.ToString)x0dx0ax0dx0a'MsgBox("全部線程結束")x0dx0ax0dx0aFor Each Str As String In newSearcher.getFilex0dx0ax0dx0atv.Nodes.Add(Str)x0dx0ax0dx0aNextx0dx0ax0dx0a'For i As Integer = 0 To newSearcher.getFile.Length - 1x0dx0ax0dx0a'tv.Nodes.Add(newSearcher.getFile(i))x0dx0ax0dx0a'Nextx0dx0ax0dx0atmr.Enabled = Falsex0dx0ax0dx0abtnExit.Enabled = Truex0dx0ax0dx0abtnOK.Enabled = Truex0dx0ax0dx0aEnd Ifx0dx0ax0dx0aMy.Application.DoEvents()x0dx0ax0dx0aEnd Subx0dx0ax0dx0a Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Clickx0dx0ax0dx0aMe.Close()x0dx0ax0dx0aEnd Sub End Class