VB怎么实现选择路径,打开路径?
我的VB中建了两个Command,一个TEXT,我要的是别人点Command1就会弹出一个框让他选择路径,他选择好路径的时候,TEXT就会显示他选择的路径,然后按Comm...
我的VB中建了两个Command,一个TEXT,我要的是别人点Command1就会弹出一个框让他选择路径,他选择好路径的时候,TEXT就会显示他选择的路径,然后按Command2就可以运行他刚刚选择的文件。
各位高手,帮忙实现`
我不能加载你说的那个部件 展开
各位高手,帮忙实现`
我不能加载你说的那个部件 展开
7个回答
推荐于2016-06-27 · 知道合伙人软件行家
关注
展开全部
VB6.0可使用CommonDialog 控件实现选择路径、打开路径。
通过使用 CommonDialog 控件的 ShowOpen 和 ShowSave
方法可显示“打开”和“另存为”对话框。
两个对话框均可用以指定驱动器,目录,文件扩展名和文件名。除对话的标题不同外,另存为对话外观上与打开对话相似
代码实例:
Private Sub Command1_Click()
' 设置“CancelError”为 True
CommonDialog1.CancelError = True
On Error GoTo ErrHandler
' 设置标志
CommonDialog1.Flags = cdlOFNHideReadOnly
' 指定缺省的过滤器
CommonDialog1.FilterIndex = 2
' 显示“打开”对话框
CommonDialog1.ShowOpen
' 显示选定文件的名字
MsgBox CommonDialog1.FileName
Exit Sub
ErrHandler:
' 用户按了“取消”按钮
Exit Sub
End Sub
展开全部
用CommonDialog控件有点缺陷,可移植性较差,就是在没装VB的电脑上可能不能运行。可使用以下API实现。
添加你说的两个Command,一个Text,name默认,拷贝以下代码到窗体代码中。
Option Explicit
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Const SW_SHOWNORMAL = 1
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Private Sub Command1_Click()
On Error Resume Next
Dim Tmp As OPENFILENAME
Dim Rtn As Long
Dim APICmdlg As String
Tmp.lStructSize = Len(Tmp)
Tmp.hwndOwner = Me.hWnd
Tmp.hInstance = App.hInstance
Tmp.lpstrFilter = "*.*"
Tmp.lpstrFile = Space(254)
Tmp.nMaxFile = 255
Tmp.lpstrFileTitle = Space(254)
Tmp.nMaxFileTitle = 255
Tmp.lpstrInitialDir = App.Path
Tmp.lpstrTitle = "打开文件"
Tmp.flags = 6148
Rtn = GetOpenFileName(Tmp)
If Rtn >= 1 Then
APICmdlg = Tmp.lpstrFile
Text1.Text = APICmdlg
End If
End Sub
Private Sub Command2_Click()
On Error Resume Next
If Dir(Text1.Text, vbHidden + vbReadOnly + vbSystem) <> "" Then
ShellExecute Me.hWnd, "open", Text1.Text, vbNullString, vbNullString, SW_SHOWNORMAL
End If
End Sub
xp+VB6.0下调试通过,希望对你有所帮助.
添加你说的两个Command,一个Text,name默认,拷贝以下代码到窗体代码中。
Option Explicit
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Const SW_SHOWNORMAL = 1
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Private Sub Command1_Click()
On Error Resume Next
Dim Tmp As OPENFILENAME
Dim Rtn As Long
Dim APICmdlg As String
Tmp.lStructSize = Len(Tmp)
Tmp.hwndOwner = Me.hWnd
Tmp.hInstance = App.hInstance
Tmp.lpstrFilter = "*.*"
Tmp.lpstrFile = Space(254)
Tmp.nMaxFile = 255
Tmp.lpstrFileTitle = Space(254)
Tmp.nMaxFileTitle = 255
Tmp.lpstrInitialDir = App.Path
Tmp.lpstrTitle = "打开文件"
Tmp.flags = 6148
Rtn = GetOpenFileName(Tmp)
If Rtn >= 1 Then
APICmdlg = Tmp.lpstrFile
Text1.Text = APICmdlg
End If
End Sub
Private Sub Command2_Click()
On Error Resume Next
If Dir(Text1.Text, vbHidden + vbReadOnly + vbSystem) <> "" Then
ShellExecute Me.hWnd, "open", Text1.Text, vbNullString, vbNullString, SW_SHOWNORMAL
End If
End Sub
xp+VB6.0下调试通过,希望对你有所帮助.
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在工具箱中的对话框类里拖拽一个openfiledialog 控件(该控件只在窗体的非显示组件格中出现)
继续拖拽一个button、一个textbox到form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myfile As String '定义一个字符串变量,用来存储文件名及路径
OpenFileDialog1.ShowDialog() '用openfiledialog的showdialog方法弹出文件浏览对话框
myfile = OpenFileDialog1.FileName '用openfiledialog的filename方法获取用户选择
'的文件名和路径并且赋值给myfile字符串变量
If My.Computer.FileSystem.FileExists(myfile) = True Then '使用my命名空间的。。。方法测试文件是否存在
TextBox1.Text = myfile '如果存在,把路径赋值给textbox1.text属性(也就是显示路径)
Dim openfiletxt As IO.StreamReader = IO.File.OpenText(myfile) '使用IO命名空间中的file.opentext方法打开文件名及路径为myfile的文本文件
'在这之间可添加对该文件的任何操作,例如:读取一行字符
openfiletxt.Close() '处理完毕,关闭打开的文件
End If
End Sub
继续拖拽一个button、一个textbox到form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myfile As String '定义一个字符串变量,用来存储文件名及路径
OpenFileDialog1.ShowDialog() '用openfiledialog的showdialog方法弹出文件浏览对话框
myfile = OpenFileDialog1.FileName '用openfiledialog的filename方法获取用户选择
'的文件名和路径并且赋值给myfile字符串变量
If My.Computer.FileSystem.FileExists(myfile) = True Then '使用my命名空间的。。。方法测试文件是否存在
TextBox1.Text = myfile '如果存在,把路径赋值给textbox1.text属性(也就是显示路径)
Dim openfiletxt As IO.StreamReader = IO.File.OpenText(myfile) '使用IO命名空间中的file.opentext方法打开文件名及路径为myfile的文本文件
'在这之间可添加对该文件的任何操作,例如:读取一行字符
openfiletxt.Close() '处理完毕,关闭打开的文件
End If
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先单击“工程”菜单,部件,勾选"Microsoft Common Dialog Control",确定,然后窗体上建一个CommonDialog1,Command1,Command2,Text1。
代码如下。
==================
Private Sub Command1_Click()
CommonDialog1.Filter = "可执行程序(*.exe)|*.exe"
CommonDialog1.ShowOpen
Text1.Text = CommonDialog1.FileName
End Sub
Private Sub Command2_Click()
Shell Text1.Text, vbNormalFocus
End Sub
代码如下。
==================
Private Sub Command1_Click()
CommonDialog1.Filter = "可执行程序(*.exe)|*.exe"
CommonDialog1.ShowOpen
Text1.Text = CommonDialog1.FileName
End Sub
Private Sub Command2_Click()
Shell Text1.Text, vbNormalFocus
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
'在窗体上加入两个Command,一个TEXT,然后在代码区复制下面代码,运行,即可看到效果。
'====窗体代码====
Option Explicit
Private Sub Command1_Click()
Dim objDialog, tfile As Long, strLoadFile As String
Set objDialog = CreateObject("UserAccounts.CommonDialog")
objDialog.Filter = "All File|*.exe"
objDialog.InitialDir = "c:\windows"
tfile = objDialog.ShowOpen
If tfile Then
strLoadFile = objDialog.FileName
Text1.Text = strLoadFile
Else
Set objDialog = Nothing
End If
End Sub
Private Sub Command2_Click()
Dim wshshell
Set wshshell = CreateObject("WScript.Shell") '创建脚本对象
wshshell.run Chr(34) & Trim(Text1.Text) & Chr(34) '运行此文件
Set wshshell = Nothing '释放内存
End Sub
'====窗体代码====
Option Explicit
Private Sub Command1_Click()
Dim objDialog, tfile As Long, strLoadFile As String
Set objDialog = CreateObject("UserAccounts.CommonDialog")
objDialog.Filter = "All File|*.exe"
objDialog.InitialDir = "c:\windows"
tfile = objDialog.ShowOpen
If tfile Then
strLoadFile = objDialog.FileName
Text1.Text = strLoadFile
Else
Set objDialog = Nothing
End If
End Sub
Private Sub Command2_Click()
Dim wshshell
Set wshshell = CreateObject("WScript.Shell") '创建脚本对象
wshshell.run Chr(34) & Trim(Text1.Text) & Chr(34) '运行此文件
Set wshshell = Nothing '释放内存
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询