Sistem Klasörlerini Öğrenmek
Programınızın kullanacağı geçici bir dosya oluşturmak istiyorsunuz. Ya da Windows'un bulunduğu klasörü programınıza bildirmek istiyorsunuz. Bu işlemleri gerçekleştirmek çok basit çünkü Windows 3 adet API kullanarak bu klasörleri bulmanıza izin veriyor. Örneğimizi uygulamak için resimde görüldüğü gibi üç adet etiket kontrolü bir adet de komut butonunu formunuz üzerine yerleştirin.
'Ayrı bir BAS modülüne girecek:
Option Explicit
Public Declare Function GetSystemDirectory
Lib "kernel32" Alias "GetSystemDirectoryA" _
(ByVal lpBuffer As String, ByVal nSize As Long) As Long
Public Declare Function GetTempPath Lib "kernel32"
Alias "GetTempPathA" _
(ByVal nSize As Long, ByVal lpBuffer As String) As Long
Public Declare Function GetWindowsDirectory
Lib "kernel32" Alias "GetWindowsDirectoryA" _
(ByVal lpBuffer As String, ByVal nSize As Long) As Long
'Formun General - Declarations bölümüne girecek:
Private Function GetSystemDir() As String
Dim r As Long
Dim nSize As Long
Dim tmp As String
tmp = Space$(256)
nSize = Len(tmp)
r = GetSystemDirectory(tmp, nSize)
GetSystemDir = TrimNull(tmp)
End Function
Public Function GetTempDir() As String
Dim r As Long
Dim nSize As Long
Dim tmp As String
tmp = Space$(256)
nSize = Len(tmp)
r = GetTempPath(nSize, tmp)
GetTempDir = TrimNull(tmp)
End Function
Private Function GetWinDir() As String
Dim r As Long
Dim nSize As Long
Dim tmp As String
tmp = Space$(256)
nSize = Len(tmp)
r = GetWindowsDirectory(tmp, nSize)
GetWinDir = TrimNull(tmp)
End Function
Private Function TrimNull(item As String)
Dim pos As Integer
pos = InStr(item, Chr$(0))
If pos Then
TrimNull = Left$(item, pos - 1)
Else: TrimNull = item
End If
End Function
Private Sub Command1_Click()
Label1 = GetWinDir()
Label2 = GetTempDir()
Label3 = GetSystemDir()
End Sub
|