主题:关于文件系统控件:为什么路径不正确可还是能执行程序呵?
大懒猫 [专家分:220] 发布于 2008-11-28 10:09:00
Private Sub Form_Load()
Form1.Caption = "文件系统控件应用实例"
Label1.Caption = "驱动器列表框"
Label2.Caption = "目录列表框"
Label3.Caption = "文件列表框"
Label4.Caption = "文件列表框中显示的文件类型:"
Label5.Caption = "在文件列表框中选中的文件:"
Combo1.Text = Combo1.List(0)
End Sub
Private Sub Drive1_Change() ' 当在驱动器列表框选择新的驱动器时
Dir1.Path = Drive1.Drive ' 目录列表框的路径改变
End Sub
Private Sub Dir1_Change() ' 目录列表框的路径改变时
File1.Path = Dir1.Path ' 文件列表框的路径改变
End Sub
Private Sub Combo1_Click()
File1.Pattern = LTrim(Right(Combo1.Text, 5)) '文件类型为组合框的后5个字符,例如*.frm
End Sub
Private Sub File1_Click()
' List1.Clear
List1.AddItem File1.FileName
End Sub
Private Sub File1_DblClick()
Dim RetVal
Dim fname As String
If Right(File1.Path, 1) = "\" Then ' 选定的目录是根目录
fname = File1.Path + File1.FileName
Else ' 选定的目录是子目录
fname = File1.Path + "\" + File1.FileName
End If
RetVal = Shell(fname, 1) ' 调用Shell函数运行程序
End Sub
大家请看:如果在File1_DblClick事件中将整个if---else---endif结构换成代码:
fname = File1.Path + "\" + File1.FileName
即对用户选择的目录不作判断,万一用户选的是根目录(如f:\)下的文件a.exe,则fname=f:\\a.exe,路径当中多了一个"\",也就是说路径错误了,那么在下一句执行shell函数时应该找不到这个可执行文件a.exe才对,但是,怎么居然也能够执行呵?!
[em10]
最后更新于:2008-12-01 12:02:00
回复列表 (共8个回复)
沙发
tanchuhan [专家分:15140] 发布于 2008-11-28 13:54:00
Shell "D:\\test.exe"
没问题,我也不知道原因。
Private Sub Form_Load()
Open "D:\\test.exe" For Binary Access Read As #1
Debug.Print LOF(1)
Close #1
End Sub
Private Sub Form_Load()
Open "D:\\temp\\test.exe" For Binary Access Read As #1
Debug.Print LOF(1)
Close #1
End
End Sub
这个也正确,莫非Windows下目录可以用双"\\"?
板凳
bcahzvip [专家分:6040] 发布于 2008-11-28 15:52:00
这个也正确,莫非Windows下目录可以用双"\\"?
恩,看来也能成立.
试试看"\\\"怎么样?
3 楼
tanchuhan [专家分:15140] 发布于 2008-11-28 23:14:00
[quote]这个也正确,莫非Windows下目录可以用双"\\"?
恩,看来也能成立.
试试看"\\\"怎么样?[/quote]
Private Sub Form_Load()
Open "D:\\\\\\\\\\temp\\\\\\\\\\\\\\\\\\\\test.exe" For Binary Access Read As #1
Debug.Print LOF(1)
Close #1
End
End Sub
[em9][em9][em9][em9]
没有任何问题
4 楼
bcahzvip [专家分:6040] 发布于 2008-11-29 00:02:00
呵呵!~
good job
5 楼
jianghongbo1 [专家分:410] 发布于 2008-12-03 16:40:00
用驱动器列表框,根本不需要判断,因drive1.drive是d: 不是d:\
6 楼
merry05 [专家分:8920] 发布于 2008-12-04 09:00:00
[quote][quote]这个也正确,莫非Windows下目录可以用双"\\"?
恩,看来也能成立.
试试看"\\\"怎么样?[/quote]
Private Sub Form_Load()
Open "D:\\\\\\\\\\temp\\\\\\\\\\\\\\\\\\\\test.exe" For Binary Access Read As #1
Debug.Print LOF(1)
Close #1
End
End Sub
[em9][em9][em9][em9]
没有任何问题[/quote]
我想Open "D:\/\/\/\/temp\/\/\/\/test.exe" For Binary Access Read As #1也应该不会有问题,记得windows的路径\/都可以用
[em66]
7 楼
tanchuhan [专家分:15140] 发布于 2008-12-04 18:28:00
[quote][quote][quote]这个也正确,莫非Windows下目录可以用双"\\"?
恩,看来也能成立.
试试看"\\\"怎么样?[/quote]
Private Sub Form_Load()
Open "D:\\\\\\\\\\temp\\\\\\\\\\\\\\\\\\\\test.exe" For Binary Access Read As #1
Debug.Print LOF(1)
Close #1
End
End Sub
[em9][em9][em9][em9]
没有任何问题[/quote]
我想Open "D:\/\/\/\/temp\/\/\/\/test.exe" For Binary Access Read As #1也应该不会有问题,记得windows的路径\/都可以用
[em66][/quote]
Private Sub Form_Load()
Open "D:\/\/\/\/temp\/\/\/\/test.exe" For Binary Access Read As #1
Debug.Print LOF(1)
Close #1
End
End Sub
pass
[em9][em9][em9][em9]
8 楼
guoyong_cy [专家分:3050] 发布于 2008-12-06 11:49:00
[quote]Shell "D:\\test.exe"
没问题,我也不知道原因。
[/quote]
还可以 Shell "D:test.exe"
看了这么多,我个人理解,“\(or/)”就是“当前目录”的意思,其本身也是一个目录,也就是说如果“d:\A”当前目录是A,“d:\A\"当前目录也是A,无论你加多少个,“d:\A\\\\"当前目录仍然是A(不过路径长度有限制哈)
我来回复