开发者

Path/File Access Error in App.Path

Well i made these codes and when i run it the vb6 said me Path/File Access Error ,开发者_StackOverflow can anyone help me:

BasePath = App.Path & "\" & "\users\"
MkDir BasePath
Open BasePath & name & "\list.txt" For Input As #1


You make c:\xxx\users\ then open c:\xxx\users\name\list.txt but you have not created the name sub directory, it wont happen automatically.

You would need to create \users, then \name. (You should probably also account for the error that will occur if you mkdir an existing directory)

Something like

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


If the directory you're creating already exists you'll get the error you describe on your call to MkDir.

I suggest first checking for the existence of the directory before attempting to create it:

If (Dir(BasePath, vbDirectory) = "") Then
   MkDir BasePath
End If


I think its the name variable that's your problem:

For example:

Suppose Basepath = "C:\Temp\users" and name = "FooBar"

MKDIR makes the path for BasePath

The Open command will try to create the path at C:\Temp\Users\FooBar\List.txt

As you've not created the FooBar subdirectory, this is why you get the File/Path Access Error


Upated with code:

I suspect that your users directory will already exist, therefore you need to create the name folder:

BasePath = App.Path & "\users\"
MkDir BasePath & name
Open BasePath & name & "\list.txt" For Input As #1
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜