VB.Net程式設計:快速獲取資料夾/檔名稱程式程式碼
產品圖片很多而且圖片的名稱都是按貨號命名的。有時候要通過圖片來獲取貨號名稱,想怎麼快速獲取幾百個圖片檔案的名稱呢。於是就寫了這個程式。
功能:選擇某一個資料夾,想獲取裡面的檔案的名稱,或者只獲取裡面的資料夾名稱,直接用這個程式。
VB.Net 2005,程式介面如:
這裡用到一個控制元件,用起來超級爽。都不用自己寫什麼就可以瀏覽電腦資源了。整個dll檔案才88K。只有用的人才知道:好用啊,做什麼資源管理程式,管理器都可以用這個控制元件了。ExpTree控制元件參考:
http://www.codeproject.com/KB/cpp/VbNetExpTree.aspx
快速獲取資料夾/檔名稱程式程式碼:
- Imports System.IO
- Imports System.Text
- Imports System.Resources
- Imports System.Reflection
- Imports ExpTreeLib
- Imports ExpTreeLib.CShItem
- Imports ExpTreeLib.SystemImageListManager
- 'ExpTree控制元件參考:
- 'http://www.codeproject.com/KB/cpp/VbNetExpTree.aspx
- Public Class FrmMain
- Dim i As Integer
- Dim CurPath As String
- Public Sub New()
- ' 此呼叫是 Windows 窗體設計器所必需的。
- InitializeComponent()
- ' 在 InitializeComponent() 呼叫之後新增任何初始化。
- SystemImageListManager.SetListViewImageList(lv1, True, False)
- SystemImageListManager.SetListViewImageList(lv1, False, False)
- End Sub
- Private Sub ShowMsg(ByVal Msg As String, Optional ByVal mType As MsgType = MsgType.Ok)
- If mType = MsgType.Ok Then
- Me.LB.ForeColor = Color.Black
- Me.LB.Text = Msg
- Else
- Me.LB.ForeColor = Color.Red
- Me.LB.Text = "出錯:" & Msg
- End If
- Me.LB.Refresh()
- End Sub
- Private Sub SetDirInfo(ByVal info As String)
- Me.LBDirInfo.Text = info
- Me.LBDirInfo.Refresh()
- End Sub
- Private Sub lv1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lv1.DoubleClick
- ExeLv1Selection()
- End Sub
- Private Sub lv1_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lv1.DragEnter
- If e.Data.GetDataPresent(DataFormats.FileDrop) Then
- e.Effect = DragDropEffects.All
- End If
- End Sub
- Private Sub lv1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lv1.DragDrop
- If e.Data.GetDataPresent(DataFormats.FileDrop) Then
- Dim MyFiles() As String
- Dim item As CShItem
- Dim sb As New StringBuilder
- MyFiles = e.Data.GetData(DataFormats.FileDrop)
- Me.TxtBoxMain.Clear()
- For i = 0 To MyFiles.Length - 1
- item = New CShItem(MyFiles(i))
- '如果拖放的是硬碟,隱藏檔案,快捷方式,系統檔案,網路硬碟,可行動硬碟就不新增到lv1中。
- If (item.IsDisk = False And item.IsLink = False And item.IsSystem = False And item.IsNetworkDrive = False And item.IsRemovable = False) Then
- Dim lvi As New ListViewItem(item.DisplayName)
- With lvi
- .ImageIndex = SystemImageListManager.GetIconIndex(item, False)
- .Tag = item
- End With
- lv1.Items.Add(lvi)
- End If
- Next
- End If
- End Sub
- Private Sub lv1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lv1.KeyDown
- If e.KeyCode = Keys.Enter Then
- ExeLv1Selection()
- ElseIf e.KeyCode = Keys.Delete Then
- If Me.lv1.SelectedItems.Count > 0 Then
- Me.lv1.Items.RemoveAt(Me.lv1.SelectedIndices(0))
- End If
- End If
- End Sub
- Private Sub ExeLv1Selection()
- If IsNothing(lv1.SelectedItems) OrElse lv1.SelectedItems.Count < 1 Then Exit Sub
- Dim item As CShItem = lv1.SelectedItems(0).Tag
- If item.IsFolder Then
- ExpTree1.ExpandANode(item)
- Else
- Try
- System.Diagnostics.Process.Start(item.Path)
- Catch ex As Exception
- Throw New Exception(ex.Message)
- End Try
- End If
- End Sub
- #Region " lv1_MouseUp 單擊開啟資料夾"
- 'Private Sub lv1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lv1.MouseUp
- 'Dim lvi As ListViewItem = lv1.GetItemAt(e.X, e.Y)
- 'If IsNothing(lvi) Then Exit Sub
- 'If IsNothing(lv1.SelectedItems) OrElse lv1.SelectedItems.Count < 1 Then Exit Sub
- 'Dim item As CShItem = lv1.SelectedItems(0).Tag
- 'If item.IsFolder Then
- ' If e.Button = Windows.Forms.MouseButtons.Left Then
- ' ExpTree1.ExpandANode(item)
- ' End If
- 'End If
- 'End Sub
- #End Region
- Private Sub BtClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtClose.Click
- Me.Close()
- Application.Exit()
- End Sub
- Private Sub BtCopyText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtCopyText.Click
- If Me.TxtBoxMain.Text > String.Empty Then
- System.Windows.Forms.Clipboard.Clear()
- System.Windows.Forms.Clipboard.SetText(Me.TxtBoxMain.Text)
- ShowMsg("文字內容已經複製在系統貼上板中")
- End If
- End Sub
- Private Sub BtCopyList2TxtBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtCopyList2TxtBox.Click
- GetLvToTxt()
- ShowMsg("共 " & Me.TxtBoxMain.Lines.Length & " 行")
- End Sub
- Private Sub GetLvToTxt()
- Me.TxtBoxMain.Clear()
- Dim sb As StringBuilder
- Dim csi As CShItem
- Dim fln As String
- If Me.lv1.Items.Count > 0 Then
- sb = New StringBuilder
- If Me.ChkBoxIsNeedExt.Checked = True Then
- For i = 0 To lv1.Items.Count - 1
- csi = CType(Me.lv1.Items(i).Tag, CShItem)
- If csi.IsFolder Then
- sb.Append(csi.DisplayName)
- sb.AppendLine()
- Else
- fln = csi.GetFileName
- If Me.TxtExtStr.Text > "" Then
- If Me.RdBtExtAll.Checked = True Then
- sb.Append(fln)
- sb.AppendLine()
- End If
- If Me.RdBtExtFliter.Checked = True Then
- If fln.IndexOf(Me.TxtExtStr.Text) = -1 Then
- sb.Append(fln)
- sb.AppendLine()
- End If
- End If
- If Me.RdBtExtOnlyGet.Checked = True Then
- If fln.IndexOf(Me.TxtExtStr.Text) > -1 Then
- sb.Append(fln)
- sb.AppendLine()
- End If
- End If
- Else
- sb.Append(fln)
- sb.AppendLine()
- End If
- End If
- Next
- Else
- For i = 0 To lv1.Items.Count - 1
- sb.Append(lv1.Items(i).Text)
- sb.AppendLine()
- Next
- End If
- Me.TxtBoxMain.Text = sb.ToString
- '減去最後一個空白行。
- Me.TxtBoxMain.Text = Mid(Me.TxtBoxMain.Text, 1, Me.TxtBoxMain.TextLength - 2)
- End If
- End Sub
- Private Sub TxtBoxMain_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtBoxMain.KeyDown
- If e.Control = True Then
- If e.KeyCode = Keys.A Then
- Me.TxtBoxMain.SelectAll()
- ElseIf e.KeyCode = Keys.C Then
- Me.TxtBoxMain.Copy()
- End If
- End If
- End Sub
- Private Sub BtComBiTxtMain_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtComBiTxtMain.Click
- If Me.TxtBoxMain.Text > "" Then
- Me.TxtBoxMain.Text = Replace(Me.TxtBoxMain.Text, Environment.NewLine, ",")
- ShowMsg("共 " & Me.TxtBoxMain.Lines.Length & " 行")
- End If
- End Sub
- Private Sub BtSplitTxtMain_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtSplitTxtMain.Click
- If Me.TxtBoxMain.Text > "" Then
- Me.TxtBoxMain.Text = Replace(Me.TxtBoxMain.Text, ",", Environment.NewLine)
- ShowMsg("共 " & Me.TxtBoxMain.Lines.Length & " 行")
- End If
- End Sub
- Private Sub LoadPathInfo(ByVal SelPath As String, ByVal CSI As ExpTreeLib.CShItem, ByVal AllFF As ShowFFType)
- Dim dirList As New ArrayList()
- Dim fileList As New ArrayList()
- Dim TotalItems As Integer
- If CSI.DisplayName.Equals(CShItem.strMyComputer) Then
- 'avoid re-query since only has dirs
- dirList = CSI.GetDirectories
- Else
- If AllFF = ShowFFType.All Then
- dirList = CSI.GetDirectories
- fileList = CSI.GetFiles
- ElseIf AllFF = ShowFFType.Folder Then
- dirList = CSI.GetDirectories
- ElseIf AllFF = ShowFFType.File Then
- fileList = CSI.GetFiles
- End If
- End If
- 'CSI.Path 如果是系統物件。就放回以::開頭的GUID
- Me.TxtFFPath.Text = SelPath
- CurPath = SelPath
- TotalItems = dirList.Count + fileList.Count
- If TotalItems > 0 Then
- Dim item As CShItem
- dirList.Sort()
- fileList.Sort()
- SetDirInfo("共: " & dirList.Count & " 目錄 " & fileList.Count & " 檔案")
- Dim combList As New ArrayList(TotalItems)
- combList.AddRange(dirList)
- combList.AddRange(fileList)
- 'Build the ListViewItems & add to lv1
- lv1.BeginUpdate()
- lv1.Items.Clear()
- lv1.Refresh()
- For Each item In combList
- Dim lvi As New ListViewItem(item.DisplayName)
- With lvi
- 'SubItem formatting and adding to lvi omitted from (從...刪除去)article text
- 'Set ListViewItem's IconIndex (and add Icon to lists if necessary)
- .ImageIndex = SystemImageListManager.GetIconIndex(item, False)
- .Tag = item
- End With
- lv1.Items.Add(lvi)
- Next
- lv1.EndUpdate()
- Else
- lv1.Items.Clear()
- SetDirInfo("0個物件")
- End If
- End Sub
- Private Sub BtOpenFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtOpenFolder.Click
- If CurPath > "" Then
- System.Diagnostics.Process.Start(CurPath)
- End If
- End Sub
- Private Sub BtClearnListView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtClearnListView.Click
- Me.lv1.Items.Clear()
- Me.lv1.Refresh()
- End Sub
- Private Sub RdBtShowFF_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RdBtShowAllFF.CheckedChanged, RdBtShowFile.CheckedChanged, RdBtShowFolder.CheckedChanged
- If CurPath > String.Empty Then
- SetDirInfo(String.Empty)
- Try
- Dim cshi As New CShItem(CurPath)
- LoadPathInfo(CurPath, cshi, GetShowFFType)
- Catch ex As Exception
- SetDirInfo("系統物件,無法過濾")
- End Try
- End If
- End Sub
- Private Function GetShowFFType() As ShowFFType
- If Me.RdBtShowAllFF.Checked Then Return ShowFFType.All
- If Me.RdBtShowFolder.Checked Then Return ShowFFType.Folder
- If Me.RdBtShowFile.Checked Then Return ShowFFType.File
- Return ShowFFType.All
- End Function
- Private Sub ExpTree1_ExpTreeNodeSelected(ByVal SelPath As System.String, ByVal Item As ExpTreeLib.CShItem) Handles ExpTree1.ExpTreeNodeSelected
- UnCheckFFType()
- LoadPathInfo(SelPath, Item, ShowFFType.All)
- End Sub
- Private Sub UnCheckFFType()
- Me.RdBtShowAllFF.Checked = True
- Me.RdBtShowFolder.Checked = False
- Me.RdBtShowFile.Checked = False
- End Sub
- Private Sub ChkBoxIsNeedExt_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkBoxIsNeedExt.CheckedChanged
- If Me.ChkBoxIsNeedExt.CheckState <> CheckState.Checked Then
- Me.RdBtExtAll.Checked = False
- Me.RdBtExtFliter.Checked = False
- Me.RdBtExtOnlyGet.Checked = False
- Me.TxtExtStr.Text = ""
- ElseIf Me.ChkBoxIsNeedExt.CheckState = CheckState.Checked Then
- Me.RdBtExtAll.Checked = True
- Me.TxtExtStr.Text = ""
- End If
- End Sub
- Private Sub BtRefrechFFList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtRefrechFFList.Click
- ExpTree1.RefreshTree()
- End Sub
- Private Sub RdBtExtAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RdBtExtAll.CheckedChanged
- If Me.RdBtExtAll.Checked = True Then
- Me.TxtExtStr.Text = ""
- End If
- End Sub
- Private Sub BtClearTxtMain_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtClearTxtMain.Click
- Me.TxtBoxMain.Clear()
- End Sub
- End Class
相關文章
- win10怎麼快速獲取資料夾內檔名 win10怎麼讀取資料夾內檔名Win10
- javascript實現的獲取檔案字尾名稱程式碼例項JavaScript
- linux檔案相關命令 透過檔案獲取父資料夾名稱Linux
- win10資料夾中怎樣批量獲取所有檔名稱_win10批量提取資料夾內檔名的方法Win10
- win10資料夾中怎樣批次獲取所有檔名稱_win10批次提取資料夾內檔名的方法Win10
- ASP.NET 8 AOT 遍歷資料夾獲取檔名稱及時長ASP.NET
- JavaScript 獲取主機名程式碼JavaScript
- PHP獲取小程式openid,10行程式碼快速獲取小程式openidPHP行程
- 程式設計師如何獲取新程式設計技能程式設計師
- 兩步快速獲取小程式原始碼原始碼
- 語種名稱程式碼
- 程式碼獲取 LaunchImage、AppIcon 圖片名稱APP
- 程式獲取Android簽名Android
- 微信小程式—— 獲取資料微信小程式
- ***批次修改資料夾名稱
- C獲取程式名稱的方法
- 控制檯應用程式獲取計算機名計算機
- php獲取訪客所在城市名稱程式碼PHP
- 程式設計師獲取程式設計靈感的 10 種方式程式設計師
- 程式設計師獲取新程式設計技能的5個技巧?程式設計師
- 只需兩步快速獲取微信小程式原始碼微信小程式原始碼
- 獲取資料夾及其子資料夾下的所有檔案
- C語言程式設計獲取PE檔案DOS頭C語言程式設計
- VB程式設計獲取檔案中整合的圖示程式設計
- Java讀取資料夾大小的6種方法及程式碼Java
- Rust 程式設計,讀取檔案Rust程式設計
- C語言程式設計規範——名稱縮寫C語言程式設計
- 教你不程式設計快速解析 JSON 資料程式設計JSON
- wpf winform 選擇檔案或選擇資料夾程式碼ORM
- Qt程式設計獲取滑鼠移動事件QT程式設計事件
- 獲取 NodeJS 程式退出碼NodeJS
- 獲取小程式二維碼
- Java 程式碼本地設定Hadoop使用者名稱密碼JavaHadoop密碼
- 3行程式碼列出硬碟上所有檔案及資料夾行程硬碟
- Delphi程式設計:用流來讀取TXT檔案中的資料程式設計
- win10開啟字型資料夾名稱亂碼怎麼回事_win10開啟字型資料夾名稱亂碼解決教程Win10
- SHELL程式設計:利用CSIDL開啟特殊資料夾 (轉)程式設計
- jquery bind()名稱空間程式碼例項jQuery