Namespace System
Namespace IO


Class FileSystemInfo
	m_CreationTime As FILETIME
	m_LastAccessTime As FILETIME
	m_LastWriteTime As FILETIME
	m_FileAttributes As DWord

	m_IsFreshed As Boolean
Protected
	FullPath As String
	OriginalPath As String
Public
	Sub FileSystemInfo()
		m_IsFreshed = False
	End Sub

	Sub ~FileSystemInfo()
	End Sub

	Override Function Equals( object As Object ) As Boolean
		If This.ToString = object.ToString Then
			Return True
		Else
			Return False
		End If
	End Function

	Override Function ToString() As String
		Return FullPath
	End Function

	'Public Properties
	Function Attributes() As DWord'FileAttributes
		If Not m_IsFreshed Then Refresh()
		Return m_FileAttributes
	End Function

	Sub Attributes(value As DWord)
		If SetFileAttributes(FullPath, value) = FALSE Then
			'Exception
			Debug
		End If
	End Sub

	Function CreationTime() As DateTime
		If Not m_IsFreshed Then Refresh()
		Return DateTime.FromFileTime(m_CreationTime)
	End Function

	Sub CreationTime(ByRef value As DateTime)
		m_CreationTime = value.ToFileTimeUtc()
		setFileTime()
	End Sub

	Function CreationTimeUtc() As DateTime
		Return CreationTime.ToUniversalTime()
	End Function

	Sub CreationTimeUtc(ByRef value As DateTime)
		CreationTime = value
	End Sub

	Function LastAccessTime() As DateTime
		If Not m_IsFreshed Then Refresh()
		Return DateTime.FromFileTime(m_LastAccessTime)
	End Function

	Sub LastAccessTime(ByRef value As DateTime)
		m_LastAccessTime = value.ToFileTimeUtc()
		setFileTime()
	End Sub

	Function LastAccessTimeUtc() As DateTime
		Return LastAccessTime.ToUniversalTime()
	End Function

	Sub LastAccessTimeUtc(ByRef value As DateTime)
		LastAccessTime = value
	End Sub

	Function LastWriteTime() As DateTime
		If Not m_IsFreshed Then Refresh()
		Return DateTime.FromFileTime(m_LastWriteTime)
	End Function

	Sub LastWriteTime(ByRef value As DateTime)
		m_LastWriteTime = value.ToFileTimeUtc()
		setFileTime()
	End Sub

	Function LastWriteTimeUtc() As DateTime
		Return LastWriteTime.ToUniversalTime()
	End Function

	Sub LastWriteTimeUtc(ByRef value As DateTime)
		LastWriteTime = value
	End Sub

	Function Exists() As Boolean
		If Not m_IsFreshed Then Refresh()
		If m_FileAttributes = 0 Then
			Return False
		Else
			Return True
		End If
	End Function

	Function Extension() As String
		Return Path.GetExtension(FullPath)
	End Function

	Function FullName() As String
		Return FullPath
	End Function

	Function Name() As String
		Return Path.GetFileName(FullPath)
	End Function

	'Public Methods
	Virtual Sub Delete()
		If DeleteFile(FullPath) = FALSE Then
			'Exception
			debug
		End If
	End Sub

	Virtual Sub Refresh()
		Dim data As WIN32_FIND_DATA
		Dim hFind = FindFirstFile(ToTCStr(FullPath), data)
		FindClose(hFind)

		m_FileAttributes = data.dwFileAttributes
		m_CreationTime = data.ftCreationTime
		m_LastAccessTime = data.ftLastAccessTime
		m_LastWriteTime = data.ftLastWriteTime

		m_IsFreshed = True
	End Sub
Private
	Sub setFileTime()
		If Not m_IsFreshed Then Refresh()
		Dim hFile = CreateFile(ToTCStr(FullPath), GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)
		If hFile = INVALID_HANDLE_VALUE Then
			debug 'Exception
			Exit Function
		End If

		If SetFileTime(hFile, m_CreationTime, m_LastAccessTime, m_LastWriteTime) = False Then
			debug 'Exception
		End If

		CloseHandle(hFile)
	End Sub
End Class


End Namespace
End Namespace
