Getting System Folders Easily via API

Finally...

And finally, here's a easy to insert list of functions you can insert to a module:

Option Explicit
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Declare Function GetWindowsDirectoryB Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Declare Function GetSystemDirectoryB Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal Path As String, ByVal cbBytes As Long) As Long
Private Const MAX_LENGTH = 512

Public Function GetWindowsSystemDirectory() As String
   Dim s As String
   Dim c As Long
   s = String$(MAX_LENGTH, 0)
   c = GetSystemDirectoryB(s, MAX_LENGTH)
   If c > 0 Then
       If c > Len(s) Then
           s = Space$(c + 1)
           c = GetSystemDirectoryB(s, MAX_LENGTH)
       End If
   End If
   GetWindowsSystemDirectory = IIf(c > 0, Left$(s, c), "")
End Function
Public Function GetWindowsDirectory() As String
   Dim s As String
   Dim c As Long
   s = String$(MAX_LENGTH, 0)
   c = GetWindowsDirectoryB(s, MAX_LENGTH)
   If c > 0 Then
       If c > Len(s) Then
           s = Space$(c + 1)
           c = GetWindowsDirectoryB(s, MAX_LENGTH)
       End If
   End If
   GetWindowsDirectory = IIf(c > 0, Left$(s, c), "")
End Function
Public Function GetTempDirectory() As String
   Dim s As String
   Dim c As Long
   s = Space$(MAX_LENGTH)
   c = GetTempPath(MAX_LENGTH, s)
   If c > 0 Then
       If c > Len(s) Then
           s = Space$(c + 1)
           c = GetTempPath(MAX_LENGTH, s)
       End If
   End If
   GetTempDirectory = IIf(c > 0, Left$(s, c), "")
End Function


Well hopefully you found my tutorial helpful. If my english doesn't make sense blame it on the booze!

As always if your stuck or want some help just post your messages on the forums or email me directly [email protected] and don't forget to include where you came from(as in the article page or subject or else i'll be lost).

You might also like...

Comments

About the author

Thushan Fernando

Thushan Fernando Australia

Senior Developer working at Readify on cool new technology and platforms.

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Measuring programming progress by lines of code is like measuring aircraft building progress by weight.” - Bill Gates