VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "FileIO"
Attribute VB_Creatable = True
Attribute VB_Exposed = True
'   FileIO class -- FILEIO.CLS
'
'   Properties
'       WindowsDirectory
'       SystemDirectory
'
'   Methods
'       TempFileName
'
'
'
Option Explicit

#If Win16 Then
Private Declare Function GetWindowsDirectory Lib "Kernel" _
    (ByVal lpBuffer As String, _
    ByVal nSize As Integer) _
    As Integer
Private Declare Function GetSystemDirectory Lib "Kernel" (ByVal lpBuffer As String, _
    ByVal nSize As Integer) As Integer
Private Declare Function GetTempDrive Lib "Kernel" (ByVal cDriveLetter As Integer) As Integer
Private Declare Function GetTempFileName Lib "Kernel" (ByVal cDriveLetter As Integer, _
    ByVal lpPrefixString As String, ByVal wUnique As Integer, _
    ByVal lpTempFileName As String) As Integer
#Else
Private Declare Function GetWindowsDirectory Lib "Kernel32" _
    Alias "GetWindowsDirectoryA" _
    (ByVal lpBuffer As String, _
    ByVal nSize As Integer) _
    As Integer
Private Declare Function GetSystemDirectory Lib "Kernel32" _
    Alias "GetSystemDirectoryA" _
    (ByVal lpBuffer As String, _
    ByVal nSize As Integer) As Integer
Private Declare Function GetTempDrive Lib "Kernel32" _
    Alias "GetTempDriveA" _
    (ByVal cDriveLetter As Integer) As Integer
Private Declare Function GetTempFileName Lib "Kernel32" _
    Alias "GetTempFileNameA" _
    (ByVal cDriveLetter As Integer, _
    ByVal lpPrefixString As String, ByVal wUnique As Integer, _
    ByVal lpTempFileName As String) As Integer
#End If

Public Function TempFileName(Optional Prefix) As String
    Dim iWorked As Integer, iTempDrive As Integer
    Dim strTempFileName As String, strPrefix As String * 3
    If IsMissing(Prefix) Then
        ' Provide a three-character prefix for the temporary file.
        strPrefix = ""
    Else
        strPrefix = Prefix
    End If
    ' Create a buffer full of spaces for GetTempFileName function.
    ' 144 is the maximum length of path/filenames in Windows 3.1.
    strTempFileName = String(144, 32)
    iWorked = GetTempDrive(iTempDrive)
    If iWorked = 0 Then GoTo errGetOLETempFileName
    iWorked = GetTempFileName(iTempDrive, strPrefix, 0, strTempFileName)
    If iWorked = 0 Then GoTo errGetOLETempFileName
    TempFileName = Trim(strTempFileName)
    Exit Function
errGetOLETempFileName:
    MsgBox "Temporary file could not be created. Check your TEMP environment " _
    & "variable in your AUTOEXEC.BAT file."
    TempFileName = ""
End Function

' Returns the Windows directory.
Public Function WindowsDirectory() As String
    Dim strWinDirectory As String
    Dim iWorked As Integer
    ' Allocate space for the returned path string.
    strWinDirectory = Space(144)
    ' Get the Windows directory.
    iWorked = GetWindowsDirectory(strWinDirectory, _
    Len(strWinDirectory))
    ' Trim off the excess space.
    WindowsDirectory = Left(strWinDirectory, iWorked)
End Function

' Returns the Windows System directory.
Public Function SystemDirectory() As String
    Dim iLen As Integer
    Dim lpBuffer As String * 256
    iLen = GetSystemDirectory(lpBuffer, 256)
    If iLen Then
        SystemDirectory = Mid$(lpBuffer, 1, iLen)
    Else
        SystemDirectory = ""
    End If
End Function

