我正在调试一个 VB6 可执行文件。可执行文件在运行时从其当前目录加载 dll 和文件。在调试器中运行时,当前目录似乎是 VB6 的目录。
如何为 VB6 设置工作目录?
这似乎不是一个“开箱即用”的解决方案。
无论如何.. 让这个话题休息.. 以下是我的 VB6 解决方案:我在我的 VB 项目“MPDEBUG”和“MPRELEASE”中定义了 2 个符号,并在我的应用程序入口点函数中调用以下函数作为第一个操作。
Public Sub ChangeDirToApp()
#If MPDEBUG = 0 And MPRELEASE = 1 Then
' assume that in final release builds the current dir will be the location
' of where the .exe was installed; paths are relative to the install dir
ChDrive App.path
ChDir App.path
#Else
' in all debug/IDE related builds, we need to switch to the "bin" dir
ChDrive App.path
ChDir App.path & BackSlash(App.path) & "..\bin"
#End If
End Sub
仅当您使用 File-Open 打开项目时,“当前目录似乎是 VB6 的目录”。
在关闭 IDE 的同时双击 .vbp 文件将其打开。
我发现可行的解决方案使用Sub Main
, 并检查程序是否在 IDE 中运行。
Dim gISIDE as Boolean
Sub Main()
If IsIDE Then
ChDrive App.Path
ChDir App.Path
End If
' The rest of the code goes here...
End Sub
Public Function IsIDE() As Boolean '
IsIDE = False
'This line is only executed if running in the IDE and then returns True
Debug.Assert CheckIDE
If gISIDE Then
IsIDE = True
End If
End Function
Private Function CheckIDE() As Boolean ' this is a helper function for Public Function IsIDE()
gISIDE = True 'set global flag
CheckIDE = True
End Function
'Declaration
Private Declare Function SetCurrentDirectory Lib "kernel32" _
Alias "SetCurrentDirectoryA" (ByVal lpPathName As String) As Long
'syntax to set current dir
SetCurrentDirectory App.Path
任何程序的当前目录 - 包括 vb6 - 都可以在快捷方式的属性中更改。我已将其更改为我的源代码树的根,它可以更快地使用 File-Open。