Getting System Folders Easily via API

Windoze Directory

An alternative to:

open "C:\Windows\System.ini" for input as #1
Input #1, strSomeString
Close #1

You can do this:

Open GetWindowsDirectory & "system.ini" for input as #1
Input #1, strSomeString
Close #1

So now when the users use it in say Windows NT 4 or Windows 2000(NT 5) you wont get any errors to this critical windows required file.

The code to achieve this is quite easy. Insert the below code into a module(or wait till the last section for all the code in one easy to use module).

Option Explicit
Private Declare Function GetWindowsDirectoryB Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Const MAX_LENGTH = 512
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

This will get the windows Directory via the 'GetWindowsDirectoryA' function in the Kernel32 API.

Press on...

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.

“Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.”