Getting System Folders Easily via API

System Directory

The most important of all directories in my opinion is the System Directory. Its vital that you dont simply use "C:\Windows\System32" or something like that if you want to get your application to work in all platforms.

So heres another snippet for your enjoyment:

Option Explicit
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

So going back to our example used before, to read say the oohhhhh so important EULA from Microsoft for your Windoze operating system:

   Dim iFileNum As Integer
   Dim strEULA as string
   iFileNum = FreeFile
   Open GetWindowsSystemDirectory& "eula.txt" For Input As iFileNum
   strEULA = Input$(LOF(iFileNum), iFileNum)
   Close #iFileNum
   msgbox strEULA, vbCritical, "UnImportant Aggreement"


Click 'Next>>' and dont collect the $100...

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.

“There's no test like production” - Anon