各位大大,我想用程序调用同级目录下的文件,因为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
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
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
不是空格的问题,而且很大部分人喜欢放在桌面。
to zou_seafarer:
试了一下app.path确实能用,但上面的原因还是不知道,不过解决了问题,把分给你吧。
好象还有一个API函数能转化为完整路径,以前见到过,现在忘记是什么函数了
不知英文的为什么没有?
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