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 / ch21code / fileio.cls < prev    next >
Text File  |  1995-08-14  |  4KB  |  116 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 (Read only)
  12. '       SystemDirectory (Read only)
  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" _
  29.         (ByVal cDriveLetter As Integer) As Integer
  30. Private Declare Function GetTempFileName Lib "Kernel" (ByVal cDriveLetter As Integer, _
  31.     ByVal lpPrefixString As String, ByVal wUnique As Integer, _
  32.     ByVal lpTempFileName As String) As Integer
  33. #Else
  34. Private Declare Function GetWindowsDirectory Lib "kernel32" _
  35.     Alias "GetWindowsDirectoryA" _
  36.     (ByVal lpBuffer As String, _
  37.     ByVal nSize As Integer) _
  38.     As Integer
  39. Private Declare Function GetSystemDirectory Lib "kernel32" _
  40.     Alias "GetSystemDirectoryA" _
  41.     (ByVal lpBuffer As String, _
  42.     ByVal nSize As Integer) As Integer
  43. Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" _
  44.     (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
  45. Private Declare Function GetTempFileName Lib "kernel32" _
  46.     Alias "GetTempFileNameA" (ByVal lpszPath As String, _
  47.     ByVal lpPrefixString As String, ByVal wUnique As Long, _
  48.     ByVal lpTempFileName As String) As Long
  49. #End If
  50.  
  51. Public Function TempFileName(Optional Prefix) As String
  52.     #If Win16 Then
  53.         Dim iWorked As Integer
  54.     #Else
  55.         Dim iWorked As Long
  56.     #End If
  57.     Dim iTempDrive As Integer
  58.     Dim strTempFileName As String, strPrefix As String * 3
  59.     If IsMissing(Prefix) Then
  60.         ' Provide a three-character prefix for the temporary file.
  61.         strPrefix = ""
  62.     Else
  63.         strPrefix = Prefix
  64.     End If
  65.     ' Create a buffer full of spaces for GetTempFileName function.
  66.     strTempFileName = String(255, 32)
  67.     #If Win16 Then
  68.         iWorked = GetTempDrive(iTempDrive)
  69.         If iWorked = 0 Then GoTo errGetOLETempFileName
  70.         iWorked = GetTempFileName(iTempDrive, strPrefix, 0, strTempFileName)
  71.     #Else
  72.         Dim lPath As Long, strPath As String
  73.         lPath = 255
  74.         strPath = String(lPath, 32)
  75.         iWorked = GetTempPath(lPath, strPath)
  76.         If iWorked = 0 Then
  77.             GoTo errGetOLETempFileName
  78.         Else
  79.             strPath = Left(strPath, iWorked)
  80.         End If
  81.         iWorked = GetTempFileName(strPath, strPrefix, 0, strTempFileName)
  82.     #End If
  83.     If iWorked = 0 Then GoTo errGetOLETempFileName
  84.     TempFileName = Trim(strTempFileName)
  85.     Exit Function
  86. errGetOLETempFileName:
  87.     MsgBox "Temporary file could not be created. "
  88.     TempFileName = ""
  89. End Function
  90.  
  91. ' Returns the Windows directory.
  92. Public Function WindowsDirectory() As String
  93.     Dim strWinDirectory As String
  94.     Dim iWorked As Integer
  95.     ' Allocate space for the returned path string.
  96.     strWinDirectory = Space(144)
  97.     ' Get the Windows directory.
  98.     iWorked = GetWindowsDirectory(strWinDirectory, _
  99.     Len(strWinDirectory))
  100.     ' Trim off the excess space.
  101.     WindowsDirectory = Left(strWinDirectory, iWorked)
  102. End Function
  103.  
  104. ' Returns the Windows System directory.
  105. Public Function SystemDirectory() As String
  106.     Dim iLen As Integer
  107.     Dim lpBuffer As String * 256
  108.     iLen = GetSystemDirectory(lpBuffer, 256)
  109.     If iLen Then
  110.         SystemDirectory = Mid$(lpBuffer, 1, iLen)
  111.     Else
  112.         SystemDirectory = ""
  113.     End If
  114. End Function
  115.  
  116.