Visual Basic Explorer
Visual Basic Explorer
 Navigation
 Home


 Coding
 Source Code

 FAQ Center

 VB Tips

 Downloads

 ToolBox

 Tutorials

 VB Games

 VB News

 VB Award

 VB Forums



 Affiliates
 Planet Source Code

 Rent a Coder

 DirectX4VB


 Misc
 Search

 Feedback

 Advertise

 About


Need to hire
a VB coder?

Please support our sponsor:

 Home 
 Site Map 
 Forums 
 News 
 Feedback 

Get Windows and System Directories

This sample will determine at run-time what the user's Windows and System directories are. Not all users have Windows neatly tucked away under a C:\Windows directory. What if they run two versions of Windows (more common right now than ever)? Hard-coding C:\Windows into your programs to work with files in the Windows directory can be dangerous at best. This tip will show you how to instruct Windows itself to tell you exactly where it is (and it's system subdirectory as well).

First thing you'll need to do is declare two functions to work with the Windows API. These two declarations looks like the following:

Private Declare Function GetWindowsDirectory _
    Lib "kernel32" Alias "GetWindowsDirectoryA" ( _
        ByVal lpBuffer As String, _
        ByVal nSize As Long) _
    As Long

Private Declare Function GetSystemDirectory _
    Lib "kernel32" Alias "GetSystemDirectoryA" ( _
        ByVal lpBuffer As String, _
        ByVal nSize As Long) _
    As Long

The next thing you will need to do is to create two FUNCTIONs. I prefer placing these in a *.BAS module but they will work fine in a form (*.FRM) as well. Note: If you place these FUNCTIONs into a *.BAS module, then you must have the DECLAREs in the same module. Neither of these functions require arguments (aren't you glad). They both will return a string value. The first of these two FUNCTIONs shown below returns the directory containing Windows while the second returns the \Windows\System subdirectory.

Private Function GetSysDir() As String
    Dim Temp As String * 256
    Dim x As Integer
    x = GetSystemDirectory(Temp, Len(Temp)) ' Make API Call (Temp will hold return value)
    GetSysDir = Left$(Temp, x)              ' Trim Buffer and return string
End Function

Private Function GetWinDir() As String
    Dim Temp As String * 256
    Dim x As Integer
    x = GetWindowsDirectory(Temp, Len(Temp)) ' Make API Call (Temp will hold return value)
    GetWinDir = Left$(Temp, x)               ' Trim Buffer and return string
End Function

Finally, call these functions whenever you need to like this:

Private Sub Form_Click()
    MsgBox GetWinDir()
    MsgBox GetSysDir()
End Sub

You can use the returned information for several things. Many software vendors maintain *.INI files in the Windows directory as opposed to the application's directory. You may need to activate Windows applets or handle application installation routines. Whatever the need is, you no longer have to assume that the user's Windows directory is in the same place as yours!

--------------------------------------------
Original tip and project submitted 
by Jeff Hargett from VB4UandMe. 
Minor text editing by Burt Abreu.
--------------------------------------------

Download VB4 Sample





Home | About | What's New | Source Code | FAQ | Tips & Tricks | Downloads | ToolBox | Tutorials | Game Programming | VB Award | Search | VB Forums | Feedback | VBNews | Copyright & Disclaimer | Advertise | Privacy Policy |

Quick searches: Site Search | Advanced Site Search 

Copyright 2002 by Exhedra Solutions, Inc.
By using this site you agree to its terms and conditions
VB Explorer and VBExplorer.com are trademarks of Exhedra Solutions, Inc.