home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Programmer'…arterly (Limited Edition) / Visual_Basic_Programmers_Journal_VB-CD_Quarterly_Limited_Edition_1995.iso / code / ch18code / getsyste.cls < prev    next >
Encoding:
Text File  |  1995-08-13  |  3.3 KB  |  101 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "FileIO"
  6. Attribute VB_Creatable = True
  7. Attribute VB_Exposed = True
  8. '   FileIO class -- FILEIO.CLS
  9. '
  10. '   Properties
  11. '       WindowsDirectory
  12. '       SystemDirectory
  13. '
  14. '   Methods
  15. '       TempFileName
  16. '
  17. '
  18. '
  19. Option Explicit
  20.  
  21. #If Win16 Then
  22. Private Declare Function GetWindowsDirectory Lib "Kernel" _
  23.     (ByVal lpBuffer As String, _
  24.     ByVal nSize As Integer) _
  25.     As Integer
  26. Private Declare Function GetSystemDirectory Lib "Kernel" (ByVal lpBuffer As String, _
  27.     ByVal nSize As Integer) As Integer
  28. Private Declare Function GetTempDrive Lib "Kernel" (ByVal cDriveLetter As Integer) As Integer
  29. Private Declare Function GetTempFileName Lib "Kernel" (ByVal cDriveLetter As Integer, _
  30.     ByVal lpPrefixString As String, ByVal wUnique As Integer, _
  31.     ByVal lpTempFileName As String) As Integer
  32. #Else
  33. Private Declare Function GetWindowsDirectory Lib "Kernel32" _
  34.     Alias "GetWindowsDirectoryA" _
  35.     (ByVal lpBuffer As String, _
  36.     ByVal nSize As Integer) _
  37.     As Integer
  38. Private Declare Function GetSystemDirectory Lib "Kernel32" _
  39.     Alias "GetSystemDirectoryA" _
  40.     (ByVal lpBuffer As String, _
  41.     ByVal nSize As Integer) As Integer
  42. Private Declare Function GetTempDrive Lib "Kernel32" _
  43.     Alias "GetTempDriveA" _
  44.     (ByVal cDriveLetter As Integer) As Integer
  45. Private Declare Function GetTempFileName Lib "Kernel32" _
  46.     Alias "GetTempFileNameA" _
  47.     (ByVal cDriveLetter As Integer, _
  48.     ByVal lpPrefixString As String, ByVal wUnique As Integer, _
  49.     ByVal lpTempFileName As String) As Integer
  50. #End If
  51.  
  52. Public Function TempFileName(Optional Prefix) As String
  53.     Dim iWorked As Integer, iTempDrive As Integer
  54.     Dim strTempFileName As String, strPrefix As String * 3
  55.     If IsMissing(Prefix) Then
  56.         ' Provide a three-character prefix for the temporary file.
  57.         strPrefix = ""
  58.     Else
  59.         strPrefix = Prefix
  60.     End If
  61.     ' Create a buffer full of spaces for GetTempFileName function.
  62.     ' 144 is the maximum length of path/filenames in Windows 3.1.
  63.     strTempFileName = String(144, 32)
  64.     iWorked = GetTempDrive(iTempDrive)
  65.     If iWorked = 0 Then GoTo errGetOLETempFileName
  66.     iWorked = GetTempFileName(iTempDrive, strPrefix, 0, strTempFileName)
  67.     If iWorked = 0 Then GoTo errGetOLETempFileName
  68.     TempFileName = Trim(strTempFileName)
  69.     Exit Function
  70. errGetOLETempFileName:
  71.     MsgBox "Temporary file could not be created. Check your TEMP environment " _
  72.     & "variable in your AUTOEXEC.BAT file."
  73.     TempFileName = ""
  74. End Function
  75.  
  76. ' Returns the Windows directory.
  77. Public Function WindowsDirectory() As String
  78.     Dim strWinDirectory As String
  79.     Dim iWorked As Integer
  80.     ' Allocate space for the returned path string.
  81.     strWinDirectory = Space(144)
  82.     ' Get the Windows directory.
  83.     iWorked = GetWindowsDirectory(strWinDirectory, _
  84.     Len(strWinDirectory))
  85.     ' Trim off the excess space.
  86.     WindowsDirectory = Left(strWinDirectory, iWorked)
  87. End Function
  88.  
  89. ' Returns the Windows System directory.
  90. Public Function SystemDirectory() As String
  91.     Dim iLen As Integer
  92.     Dim lpBuffer As String * 256
  93.     iLen = GetSystemDirectory(lpBuffer, 256)
  94.     If iLen Then
  95.         SystemDirectory = Mid$(lpBuffer, 1, iLen)
  96.     Else
  97.         SystemDirectory = ""
  98.     End If
  99. End Function
  100.  
  101.