vb.net 如何打开listbox被选中文件夹
listbox中有好几个文件,同时显示文件所在的路径,例如e:\zbzh\2001\test.txt,我现在想选中一个文件,点击一个按钮,就自动打开该文件的文件夹,怎么做...
listbox中有好几个文件,同时显示文件所在的路径,例如e:\zbzh\2001\test.txt,我现在想选中一个文件,点击一个按钮,就自动打开该文件的文件夹,怎么做?
展开
2个回答
展开全部
'在窗体启动时,给列表初始化,测试用,随便添加了几条列表项,并将列表的扩展多选功能打开。
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.Add("c:\windows\notepad.exe")
ListBox1.Items.Add("c:\windows\system32\notepad.exe")
ListBox1.Items.Add("C:\WINDOWS\Web\Wallpaper\Bliss.bmp")
ListBox1.Items.Add("c:\windows\system32\drivers")
ListBox1.Items.Add("c:\dddddd")
ListBox1.Items.Add("c:\dos\2\3.txt")
ListBox1.SelectionMode = SelectionMode.MultiExtended
End Sub
'打开列表中所有选中的文件或者文件夹,并判断是否存在。
'如果是文件夹直接打开,如果是文件,打开目录后,并自动选中该文件
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
Dim pr As Process = New Process
Dim curFile As String
For i = 0 To ListBox1.SelectedItems.Count - 1
pr.StartInfo.FileName = "explorer.exe"
curFile = ListBox1.SelectedItems(i)
If System.IO.Directory.Exists(curFile) Then
pr.StartInfo.Arguments = Chr(34) & curFile & Chr(34)
Else
If System.IO.File.Exists(curFile) Then
pr.StartInfo.Arguments = "/n,/select," & Chr(34) & ListBox1.SelectedItems(i) & Chr(34)
Else
Continue For
End If
End If
pr.Start()
Next
pr = Nothing
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.Add("c:\windows\notepad.exe")
ListBox1.Items.Add("c:\windows\system32\notepad.exe")
ListBox1.Items.Add("C:\WINDOWS\Web\Wallpaper\Bliss.bmp")
ListBox1.Items.Add("c:\windows\system32\drivers")
ListBox1.Items.Add("c:\dddddd")
ListBox1.Items.Add("c:\dos\2\3.txt")
ListBox1.SelectionMode = SelectionMode.MultiExtended
End Sub
'打开列表中所有选中的文件或者文件夹,并判断是否存在。
'如果是文件夹直接打开,如果是文件,打开目录后,并自动选中该文件
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
Dim pr As Process = New Process
Dim curFile As String
For i = 0 To ListBox1.SelectedItems.Count - 1
pr.StartInfo.FileName = "explorer.exe"
curFile = ListBox1.SelectedItems(i)
If System.IO.Directory.Exists(curFile) Then
pr.StartInfo.Arguments = Chr(34) & curFile & Chr(34)
Else
If System.IO.File.Exists(curFile) Then
pr.StartInfo.Arguments = "/n,/select," & Chr(34) & ListBox1.SelectedItems(i) & Chr(34)
Else
Continue For
End If
End If
pr.Start()
Next
pr = Nothing
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Dim a As Integer
Dim b As Integer
Dim c As String
MsgBox(ListBox1.SelectedItem.ToString)
1:
Shell("cmd /c start c:\")
a = InStr(a + 1, ListBox1.SelectedItem.ToString, "\")
If a <> 0 Then b = a : GoTo 1
c = ListBox1.SelectedItem.ToString
Shell("cmd /c start " & Microsoft.VisualBasic.Left(c, b))
Dim b As Integer
Dim c As String
MsgBox(ListBox1.SelectedItem.ToString)
1:
Shell("cmd /c start c:\")
a = InStr(a + 1, ListBox1.SelectedItem.ToString, "\")
If a <> 0 Then b = a : GoTo 1
c = ListBox1.SelectedItem.ToString
Shell("cmd /c start " & Microsoft.VisualBasic.Left(c, b))
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询