vb6打开文件以追加问题路径未找到

时间:2008-11-16 02:28:39

标签: vb6 append

Open  App.Path & "\Folder\" & str(0) For Output

似乎找不到找到的路径,但是如果直接在我之前

MsgBox App.Path & "\Folder\" & str(0)

它提供我想要的正确目录/文件名

如果我用引号中的直接路径替换该字符串,它可以正常工作但对我的应用程序的其他用户来说不是很好:(任何人都知道为什么这不起作用?

3 个答案:

答案 0 :(得分:2)

您可以打开不存在的文件。我尝试过:

  Open "c:\temp\test.txt" & Str(0) For Output As #1
  Close #1

当它运行时,它创建了c:\ temp \ test.txt 0

请注意,我在Open语句中添加了“As#1”,并且taht Str(0)为可选的减号添加了一个前导空格(CStr(0)不添加前导空格)

答案 1 :(得分:2)

注释:您可以打开不存在的文件。

仅当您的文件夹存在时才为true。如果您的文件夹和文件都不存在,则会出现“找不到路径”错误。

答案 2 :(得分:0)

这里有一些我很容易找到的东西:

Function CreateLog(Destination As String, MyMessage As String)
    Dim PathToCreate, FolderPath, FileName As String

    'Check for Unnecessary Spaces
    Destination = Trim(Destination)
    FolderStr = Destination

    'Gather only FolderPath of Destination
    Do
        FolderStr = Mid(FolderStr, 1, Len(FolderStr) - 1)
    Loop Until Right(FolderStr, 1) = "\" Or Len(FolderStr) < 1

    'Gather only FileName
    FileName = Mid(Destination, Len(FolderStr) + 1, Len(Destination) - Len(FolderStr))

    'If the path does not exist than create it
    'Recursive approach
    For Each Folder In Split(FolderStr, "\")
        If InStr(1, Folder, ":") = 0 Then
            PathToCreate = PathToCreate & "\" & Folder
        Else
            PathToCreate = Folder
        End If
        If fso.FolderExists(PathToCreate) = False And PathToCreate <> "" Then
            fso.CreateFolder PathToCreate
        End If
    Next

    'Open file and add the message in it
    Open PathToCreate & "\" & FileName & ".txt" For Append As #1
    Print #1, MyMessage
    Close #1

End Function

用法:

CreateLog "D:\Test\NewTest\NewFolder\AnotherFolder\atlastthefile.abcdefg", "Hello!"

无论发生什么文件延长导致生病了添加“.txt”无论如何..