我做了这些代码,当我运行它时,vb6说我Path/File Access Error
,任何人都可以帮助我:
BasePath = App.Path & "\" & "\users\"
MkDir BasePath
Open BasePath & name & "\list.txt" For Input As #1
答案 0 :(得分:3)
您使c:\xxx\users\
然后打开c:\xxx\users\name\list.txt
但您尚未创建name
子目录,它不会自动发生。
您需要创建\users
,然后\name
。 (您可能还应该考虑到mkdir
现有目录时会出现的错误)
类似
sub foo
Dim BasePath As String
Dim name As String: name = "bob"
'// get App.Path accounting for "DRIVE:\" which has a trailing \
Dim root As String: root = App.Path & IIf(Right$(App.Path, 1) <> "\", "\", "")
BasePath = root & "users\"
makeDir BasePath
makeDir BasePath & name & "\"
'//you have this:
Open BasePath & name & "\list.txt" For Input As #1
'//but if you have just created the directory, the file wont exist so this will error?
end sub
Sub makeDir(sPath As String)
If Len(Dir(sPath, vbDirectory)) = 0 Then MkDir sPath
End Sub
答案 1 :(得分:2)
如果您创建的目录已存在,则会在您致电MkDir
时收到您所描述的错误。
我建议在尝试创建目录之前先检查目录是否存在:
If (Dir(BasePath, vbDirectory) = "") Then
MkDir BasePath
End If
答案 2 :(得分:1)
我认为name
变量就是你的问题:
例如:
假设Basepath = "C:\Temp\users"
和name = "FooBar"
MKDIR
为BasePath创建路径
Open
命令将尝试在C:\Temp\Users\FooBar\List.txt
由于您尚未创建FooBar
子目录,这就是您获得File/Path Access Error
上传代码:
我怀疑您的users
目录已经存在,因此您需要创建名称文件夹:
BasePath = App.Path & "\users\"
MkDir BasePath & name
Open BasePath & name & "\list.txt" For Input As #1