Комплект лабораторных работ / Лабораторная работа3
.docСанкт-Петербургский
Государственный электротехнический университет
Отчет
по лабораторной работе №3
«Файловая система»
Выполнил: Солоха В.Н.
Группа:0331
Факультет КТИ
Санкт-Петербург
2003г.
Цель работы: Определение свойств файлов, размер , тип и атрибуты.
Используемые API функции:
Private Declare Function GetBinaryType& Lib "kernel32" Alias _
"GetBinaryTypeA" (ByVal lpApplicationName As String, _
lpBinaryType As Long)
-Проверяет, является ли файл исполняемым.
Private Declare Function GetFileAttributes& Lib "kernel32" Alias _
"GetFileAttributesA" (ByVal lpFileName As String)
-Получает атрибуты файла.
Private Declare Function FindFirstFile Lib "kernel32" Alias _
"FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
-Функция упрощает поиск файла.
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
-Функция упрощает закрытие файла.
Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
-Заполняет структуру SYSTEMTIME временем, заданным структурой FILETIME.
Интерфейс программы:
Текст программы:
Begin VB.Form Form3
BorderStyle = 1 'Fixed Single
Caption = "Работа с файлами"
ClientHeight = 3255
ClientLeft = 45
ClientTop = 330
ClientWidth = 8055
LinkTopic = "Form3"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 3255
ScaleWidth = 8055
StartUpPosition = 2 'CenterScreen
Begin VB.Frame Frame1
Caption = " Информация об файле "
Height = 3015
Left = 4080
TabIndex = 3
Top = 120
Width = 3855
Begin VB.Label Label3
BorderStyle = 1 'Fixed Single
Caption = "---"
Height = 1335
Left = 120
TabIndex = 6
Top = 720
Width = 3615
End
Begin VB.Label Label2
Caption = "---"
Height = 255
Left = 120
TabIndex = 5
Top = 480
Width = 3615
End
Begin VB.Label Label1
Alignment = 2 'Center
Caption = "Файл не выбран"
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 204
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = 120
TabIndex = 4
Top = 240
Width = 3615
End
End
Begin VB.FileListBox File1
Height = 3015
Left = 1920
TabIndex = 2
Top = 100
Width = 2055
End
Begin VB.DirListBox Dir1
Height = 2565
Left = 120
TabIndex = 1
Top = 520
Width = 1695
End
Begin VB.DriveListBox Drive1
Height = 315
Left = 120
TabIndex = 0
Top = 120
Width = 1695
End
End
Attribute VB_Name = "Form3"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Declare Function GetBinaryType& Lib "kernel32" Alias _
"GetBinaryTypeA" (ByVal lpApplicationName As String, _
lpBinaryType As Long)
Private Declare Function GetFileAttributes& Lib "kernel32" Alias _
"GetFileAttributesA" (ByVal lpFileName As String)
Private Declare Function FindFirstFile Lib "kernel32" Alias _
"FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * 260
cAlternate As String * 14
End Type
Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Private Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
Private Sub File1_Click()
Label1.Caption = File1.FileName
Label2.Caption = File1.Path
Label3.Caption = "Тип: " + BinaryType()
End Sub
Private Function BinaryType() As String
Dim res, znac, hFile, RetVal As Long
Dim tmp$, str$
Dim Win32Data As WIN32_FIND_DATA
If Len(File1.Path) = 3 Then
tmp = File1.Path + File1.FileName
Else
tmp = File1.Path + "\" + File1.FileName
End If
res = GetBinaryType(tmp, scs_32bit_binary)
If res <> 0 Then
str = "Исполняемый файл WIN32"
Else
str = "Не исполняемый файл"
End If
znac = GetFileAttributes(tmp)
Select Case znac
Case 128
str = str + vbCrLf + "Атрибуты не установлены"
Case 32
str = str + vbCrLf + "Атрибуты: Архивный"
Case 2
str = str + vbCrLf + "Атрибуты: Скрытый"
Case 34
str = str + vbCrLf + "Атрибуты: Скрытый и Архивный"
Case 1
str = str + vbCrLf + "Атрибуты: Только Чтение"
Case 33
str = str + vbCrLf + "Атрибуты: Только Чтение и Архивный"
Case 3
str = str + vbCrLf + "Атрибуты: Только Чтение и Скрытый"
Case 35
str = str + vbCrLf + "Атрибуты: Только Чтение, Скрытый и Архивный"
End Select
hFile = FindFirstFile(tmp, Win32Data)
str = str + vbCrLf + "Размер файла: " + CStr(Format(Win32Data.nFileSizeLow, "#,0")) + " Байт"
str = str + vbCrLf + "Создан: " + FormatDate(Win32Data.ftCreationTime)
str = str + vbCrLf + "Изменен: " + FormatDate(Win32Data.ftLastWriteTime)
str = str + vbCrLf + "Открыт: " + FormatDate(Win32Data.ftLastAccessTime)
RetVal = FindClose(hFile)
BinaryType = str
End Function
Private Function FormatDate(FT As FILETIME) As String
Dim Dte As SYSTEMTIME
Dim res As Long
Dim str As String
res = FileTimeToSystemTime(FT, Dte)
str = Format(CStr(Dte.wDay) + "-" + CStr(Dte.wMonth) + "-" + CStr(Dte.wYear), "Long Date") + " " _
+ Format(CStr(Dte.wHour + 4) + ":" + CStr(Dte.wMinute) + ":" + CStr(Dte.wSecond), "Long Time")
FormatDate = str
End Function