各位大大,我想用程序调用同级目录下的文件,因为vb没有像C那样方便的fopen("test.txt","r")用引号打开当前目录文件的方法,所以只好用以下程序先取得当前目录在连接上\test,但问题是在英文下没问题在中文下就不行了,实在不知为何,现象如下:
current directory = c:\ 则 msgbox显示 c:\test
current directory = c:\documents and settings\..\桌面 则msgbox 仍显示 c:\documents and settings\..\桌面
我打印了连接前后的strPath发现length 已经加了5但为什么字符串仍没变呢,这个问题想了很久始终不明白,但又没其它好方法希望帮忙看看!!
===========================================================================
Private Declare Function GetCurrentDirectoryA Lib "kernel32.dll" (ByVal length As Long, ByVal buffer As String) As Long
Private Sub Form_Load()
    Dim strPath As String
    Dim lnum As Long
    
    strPath = Space(255)
    lnum = GetCurrentDirectoryA(255, strPath)
    strPath = Left(strPath, lnum)
    If Right(strPath, 1) = "\" Then
        strPath = strPath & "test"
    Else
        strPath = strPath & "\test"
    End If
    MsgBox strPath
End Sub

解决方案 »

  1.   

    那是因为你的strpath中含有一个肉眼看不到的 Chr(0) 被它挡住了后面追加的东东Private Declare Function GetCurrentDirectoryA Lib "kernel32.dll" (ByVal length As Long, ByVal buffer As String) As Long
    Private Sub Form_Load()
       Dim strPath$, lnum&
       strPath = Space(255)
       lnum = GetCurrentDirectoryA(255, strPath)
       strPath = Left(strPath, lnum)
       If InStr(strPath, Chr(0)) > 0 Then strPath = Mid(strPath, 1, InStr(strPath, Chr(0)) - 1)
       strPath = IIf(Right(strPath, 1) = "\", strPath & "test", strPath & "\test")
       MsgBox strPath
    End Sub'*************** 取得本地路径 app.path 我的方法如下Dim strPath$
    Private Sub Form_Load()
       strPath = IIf(Right(App.Path, 1) = "\", App.Path & "test.txt", App.Path & "\test.txt")
       MsgBox strPath
    End Sub
      

  2.   

    to zuoxingyu:
    不是空格的问题,而且很大部分人喜欢放在桌面。
    to zou_seafarer:
    试了一下app.path确实能用,但上面的原因还是不知道,不过解决了问题,把分给你吧。
      

  3.   

    你取的路径是缩写路径,并非完整路径!
    好象还有一个API函数能转化为完整路径,以前见到过,现在忘记是什么函数了
      

  4.   

    to:cbm666
    不知英文的为什么没有?
      

  5.   

    回4F就是空格 chr(0) 的问题, 累似你这用法的API 还有取得windows目录的API都一样会有这个问题.如果你要放桌面的话给你一个最简单的方法Dim strPath$
    Private Sub Form_Load()
       strPath = Environ("userprofile") & "\桌面\test.txt"
       MsgBox strPath
    End Sub'***** 回 5F, 你说的就是 GetShortPathName 这个API, 如果你在DOS下使用长路径请先用这转为短路径名Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
    Dim strPath$
    Private Sub Form_Load()
       strPath = GetShortName(Environ("userprofile")) & "\桌面\test.txt"
       MsgBox strPath
    End SubPublic Function GetShortName(ByVal sLongFileName As String) As String
       Dim lRetVal&, sShortPathName$
       sShortPathName = Space(255)
       Call GetShortPathName(sLongFileName, sShortPathName, 255)
       GetShortName = Mid(sShortPathName, 1, InStr(sShortPathName, Chr(0)) - 1)
    End Function
      

  6.   

    回 8F 的, 我不敢确定英文的有没有, 你试一下这看会不会大于 0, 大于0就有,(我个人认为应该会有,总之试看吧)msgbox InStr(strPath, Chr(0)) 
      

  7.   

    Public Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long