+ Ve ban chat C# rat manh. Nhung thuc te C# cung nhu cac ngon ngu lap trinh khac tren nen .Net nhu VB.Net, J++, C++. Neu cac ban tim hieu no len tim hieu cac tools cua ngon ngu nay vi du: NetframeWord 3.5. C la ngon ngu chu yeu dung trong lap trinh he thong nhung toi co the viet mot chuong trinh NC for Win bang VB.Net ma van manh nhu C#. C# la ngon ngu su dung cu phap nhu C hay Java len se hoi kho hieu khi bat dau tim hieu.
+
Sau day la ma nguon chuong trinh NC for Win viet bang VB.Net: Imports System.IO
Imports System.IO.Compression
Imports NC3.FileSystemImageList
Public Class MainForm
Public ItemTable As New DataTable
Public ActiveView As ListView
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim v As ListView
For i As Integer = 1 To 2
If i = 1 Then v = LeftView Else v = RightView
v.Columns.Add("Tên", 120, HorizontalAlignment.Left)
v.Columns.Add("Mở rộng", 50, HorizontalAlignment.Right)
v.Columns.Add("Kích thước", 50, HorizontalAlignment.Right)
v.Columns.Add("Thời gian", 120, HorizontalAlignment.Right)
v.Items.Clear()
v.FullRowSelect = True
v.View = View.Details
Next
ItemTable.Columns.Add("Name")
ItemTable.Columns.Add("Ext")
ItemTable.Columns.Add("Size")
ItemTable.Columns.Add("Time")
ItemTable.Columns.Add("FullPath")
For Each Drive As DriveInfo In My.Computer.FileSystem.Drives
If Drive.IsReady Then
tscLeftDrive.Items.Add(String.Format("{0} - {1}", Drive.Name, Drive.VolumeLabel))
tscRightDrive.Items.Add(String.Format("{0} - {1}", Drive.Name, Drive.VolumeLabel))
End If
Next
tscLeftDrive.SelectedIndex = 0
tscRightDrive.SelectedIndex = 0
lbPath.Text = Application.StartupPath & ">"
AddHandler LeftView.KeyDown, AddressOf ListView_KeyDown
AddHandler RightView.KeyDown, AddressOf ListView_KeyDown
AddHandler LeftView.GotFocus, AddressOf ListView_GotFocus
AddHandler RightView.GotFocus, AddressOf ListView_GotFocus
AddHandler LeftView.ItemActivate, AddressOf ListView_ItemActivate
AddHandler RightView.ItemActivate, AddressOf ListView_ItemActivate
AddHandler LeftView.SelectedIndexChanged, AddressOf ListView_SelectedIndexChanged
AddHandler RightView.SelectedIndexChanged, AddressOf ListView_SelectedIndexChanged
LeftView.Focus()
End Sub
Public Sub LoadFolderAndFiles(ByVal rootPath As String, ByVal view As ListView, Optional ByVal sortBy As SortColumn = SortColumn.isName, Optional ByVal FilterMark As String = "*.*")
view.Items.Clear()
Dim item As ListViewItem
Dim folders As String() = IO.Directory.GetDirectories(rootPath, FilterMark)
Dim files As String() = IO.Directory.GetFiles(rootPath, FilterMark)
Dim FileSystemImages As New FileSystemImageList
view.SmallImageList = FileSystemImages.SmallImageList
item = New ListViewItem("[..]")
item.ImageIndex = FileSystemImageList.FolderOpenIconIndex
item.Tag = rootPath
view.Items.Add(item)
ItemTable.Rows.Clear()
For Each folder As String In folders
Dim DirInfo As DirectoryInfo = New DirectoryInfo(folder)
Dim ext As String = DirInfo.Extension
Dim createDate As String = DirInfo.CreationTime
Dim r As DataRow = ItemTable.NewRow
r("Name") = IO.Path.GetFileNameWithoutExtension(folder)
r("Ext") = ext
r("Size") = ""
r("Time") = createDate
r("FullPath") = folder
ItemTable.Rows.Add(r)
Next
If sortBy <> SortColumn.isUnsort Then ItemTable.DefaultView.Sort = ItemTable.Columns(sortBy).ColumnName
For Each r As DataRowView In ItemTable.DefaultView
Dim FolderName As String = r("Name")
item = New ListViewItem(FolderName)
item.Tag = r("FullPath")
item.ImageIndex = FileSystemImageList.FolderClosedIconIndex
item.SubItems.Add(r("Ext"))
item.SubItems.Add(r("Size"))
item.SubItems.Add(r("Time"))
view.Items.Add(item)
Next
ItemTable.Rows.Clear()
For Each fileName As String In files
Dim fInfo As FileInfo = New FileInfo(fileName)
Dim ext As String = fInfo.Extension
Dim Size As String = fInfo.Length \ 1000
Dim createDate As String = fInfo.CreationTime
Dim r As DataRow = ItemTable.NewRow
r("Name") = IO.Path.GetFileName(fileName)
r("Ext") = ext
r("Size") = Size
r("Time") = createDate
r("FullPath") = fileName
ItemTable.Rows.Add(r)
Next
If sortBy <> SortColumn.isUnsort Then ItemTable.DefaultView.Sort = ItemTable.Columns(sortBy).ColumnName
For Each r As DataRowView In ItemTable.DefaultView
Dim FileName As String = r("Name")
item = New ListViewItem(FileName)
item.ImageIndex = FileSystemImages.GetTheFileIconIndex(FileName)
item.Tag = r("FullPath")
item.SubItems.Add(r("Ext"))
item.SubItems.Add(r("Size"))
item.SubItems.Add(r("Time"))
view.Items.Add(item)a
Next
If view Is LeftView Then
tsLeftStatusBar.Text = String.Format("Tổng {0} Thư mục/ {1} Files", folders.Length, files.Length)
Else
tsRightStatusBar.Text = String.Format("Tổng {0} Thư mục/ {1} Files", folders.Length, files.Length)
End If
view.Tag = rootPath
If view Is ActiveView Then lbPath.Text = ActiveView.Tag & ">"
End Sub
Private Sub ListView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
Select Case e.KeyCode
Case Keys.Oemplus
Case Keys.Insert
Dim v As ListView = sender
For i As Integer = 0 To v.SelectedItems.Count - 1
Dim item As ListViewItem = v.SelectedItems(i)
If item.ForeColor = Color.Black Then
item.ForeColor = Color.Red
item.Font = New Font(Me.Font, FontStyle.Bold)
Else
item.ForeColor = Color.Black
item.Font = Me.Font
End If
Next
End Select
End Sub
Private Sub ListView_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
ActiveView = sender
lbPath.Text = ActiveView.Tag & ">"
End Sub
Private Sub ListView_ItemActivate(ByVal sender As Object, ByVal e As System.EventArgs)
Dim item As ListViewItem = sender.FocusedItem
If item.Tag IsNot Nothing Then
Select Case item.ImageIndex
Case FileSystemImageList.FolderClosedIconIndex
LoadFolderAndFiles(item.Tag, sender)
Directory.SetCurrentDirectory(sender.Tag)
lbPath.Text = sender.Tag & ">"
Case FileSystemImageList.FolderOpenIconIndex
Dim parentFolder As DirectoryInfo = IO.Directory.GetParent(item.Tag)
If parentFolder IsNot Nothing Then
LoadFolderAndFiles(parentFolder.FullName, sender)
Directory.SetCurrentDirectory(sender.Tag)
lbPath.Text = sender.Tag & ">"
End If
Case Else
Try
Process.Start(item.Tag)
Catch ex As Exception
MsgBox("Không thể mở File")
End Try
End Select
End If
End Sub
Private Sub ListView_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim item As ListViewItem = sender.FocusedItem
If item.Tag IsNot Nothing Then
LabelStatusBar.Text = item.Tag
End If
End Sub
Private Sub tscLeftDrive_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles tscLeftDrive.SelectedIndexChanged
Dim drive As DriveInfo = My.Computer.FileSystem.Drives(sender.SelectedIndex)
LoadFolderAndFiles(drive.Name, LeftView)
Dim FreeSpace As String = Format(drive.TotalFreeSpace \ 1000000, "###,###,###")
Dim UsedSpace As String = Format((drive.TotalSize - drive.TotalFreeSpace) \ 1000000, "###,###,###")
tsLeftDriveStatus.Text = String.Format("{0}M Trống - {1}M Sử dụng", FreeSpace, UsedSpace)
End Sub
Private Sub tscRightDrive_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles tscRightDrive.SelectedIndexChanged
Dim drive As DriveInfo = My.Computer.FileSystem.Drives(sender.SelectedIndex)
LoadFolderAndFiles(drive.Name, RightView)
Dim FreeSpace As String = Format(drive.TotalFreeSpace \ 1000000, "###,###,###")
Dim UsedSpace As String = Format((drive.TotalSize - drive.TotalFreeSpace) \ 1000000, "###,###,###")
tsRightDriveStatus.Text = String.Format("{0}M Trống - {1}M Sử dụng", FreeSpace, UsedSpace)
End Sub
Private Sub txtCommand_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtCommand.KeyUp
Select Case e.KeyCode
Case Keys.Enter
Dim start_info As New ProcessStartInfo("cmd.exe")
start_info.Arguments = "/c" & txtCommand.Text
start_info.UseShellExecute = False
start_info.CreateNoWindow = True
start_info.RedirectStandardOutput = True
start_info.RedirectStandardError = True
Dim proc As New Process()
proc.StartInfo = start_info
proc.Start()
Dim std_out As StreamReader = proc.StandardOutput()
Dim std_err As StreamReader = proc.StandardError()
DOSForm.txtResult.Text = std_out.ReadToEnd()
Dim sError As String = std_err.ReadToEnd()
DOSForm.txtResult.AppendText(sError)
If DOSForm.txtResult.Text <> "" Then DOSForm.Show()
std_out.Close()
std_err.Close()
proc.Close()
End Select
End Sub
Private Sub mnLeftOnOf_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnLeftOnOf.Click
Me.SplitContent.Panel1Collapsed = Not Me.SplitContent.Panel1Collapsed
End Sub
Private Sub mnLeftBrieft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnLeftBrieft.Click
Me.LeftView.View = View.List
End Sub
Private Sub mnLeftFull_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnLeftFull.Click
Me.LeftView.View = View.Details
End Sub
Private Sub mnLeftRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnLeftRefresh.Click
LoadFolderAndFiles(LeftView.Tag, LeftView)
End Sub
Private Sub mnLeftUnsort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnLeftUnsort.Click
LoadFolderAndFiles(LeftView.Tag, LeftView, SortColumn.isUnsort)
End Sub
Private Sub LeftView_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles LeftView.ColumnClick
LoadFolderAndFiles(LeftView.Tag, LeftView, e.Column)
End Sub
Public Enum SortColumn
isName = 0
isExt = 1
isSize = 2
isTime = 3
isUnsort = 4
End Enum
Private Sub mnLeftName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnLeftName.Click
LoadFolderAndFiles(LeftView.Tag, LeftView, SortColumn.isName)
End Sub
Private Sub mnLeftExt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnLeftExt.Click
LoadFolderAndFiles(LeftView.Tag, LeftView, SortColumn.isExt)
End Sub
Private Sub mnLeftTime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnLeftTime.Click
LoadFolderAndFiles(LeftView.Tag, LeftView, SortColumn.isTime)
End Sub
Private Sub mnLeftSize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnLeftSize.Click
LoadFolderAndFiles(LeftView.Tag, LeftView, SortColumn.isSize)
End Sub
Private Sub mnView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnView.Click
Process.Start("notepad", ActiveView.FocusedItem.Tag)
End Sub
Private Sub mnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnEdit.Click
Process.Start(ActiveView.FocusedItem.Tag)
End Sub
Private Sub mnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnFind.Click
Dim FilterMark As String = InputBox("Tên File ", "File cần tìm", "")
If FilterMark <> "" Then
LoadFolderAndFiles(ActiveView.Tag, ActiveView, SortColumn.isName, FilterMark)
End If
End Sub
Private Sub mnCompare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnCompare.Click
CompareLeftRightFolders()
End Sub
Private Sub SelectAll(ByVal isAll As Boolean)
For Each item As ListViewItem In ActiveView.Items
If isAll Then
item.ForeColor = Color.Red
item.Font = New Font(Me.Font, FontStyle.Bold)
Else
item.ForeColor = Color.Black
item.Font = Me.Font
End If
Next
End Sub
Private Sub CompareLeftRightFolders()
LoadFolderAndFiles(LeftView.Tag, LeftView, SortColumn.isName)
LoadFolderAndFiles(RightView.Tag, RightView, SortColumn.isName)
For Each LeftItem As ListViewItem In LeftView.Items
Dim item As ListViewItem = RightView.FindItemWithText(LeftItem.Text)
If item Is Nothing Then
LeftItem.ForeColor = Color.DarkRed
LeftItem.Font = New Font(Me.Font, FontStyle.Bold)
End If
Next
For Each RightItem As ListViewItem In RightView.Items
Dim item As ListViewItem = LeftView.FindItemWithText(RightItem.Text)
If item Is Nothing Then
RightItem.ForeColor = Color.DarkRed
RightItem.Font = New Font(Me.Font, FontStyle.Bold)
End If
Next
End Sub
Private Sub mnSelectAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnSelectAll.Click
SelectAll(True)
End Sub
Private Sub mnDeselectAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnDeselectAll.Click
SelectAll(False)
End Sub
Private Sub mnRightOnOf_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnRightOnOf.Click
Me.SplitContent.Panel1Collapsed = Not Me.SplitContent.Panel1Collapsed
End Sub
Private Sub mnRightBrief_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnRightBrief.Click
Me.RightView.View = View.List
End Sub
Private Sub mnRightFull_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnRightFull.Click
Me.RightView.View = View.Details
End Sub
Private Sub mnRightRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnRightRefresh.Click
LoadFolderAndFiles(RightView.Tag, RightView)
End Sub
Private Sub mnRightUnsort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnRightUnsort.Click
LoadFolderAndFiles(RightView.Tag, RightView, SortColumn.isUnsort)
End Sub
Private Sub mnRightName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnRightName.Click
LoadFolderAndFiles(RightView.Tag, RightView, SortColumn.isName)
End Sub
Private Sub mnRightExt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnRightExt.Click
LoadFolderAndFiles(RightView.Tag, RightView, SortColumn.isExt)
End Sub
Private Sub mnRightTime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnRightTime.Click
LoadFolderAndFiles(RightView.Tag, RightView, SortColumn.isTime)
End Sub
Private Sub mnRightSize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnRightSize.Click
LoadFolderAndFiles(RightView.Tag, RightView, SortColumn.isSize)
End Sub
Private Sub ZipFile(ByVal source As String, ByVal Dest As String)
Dim fsWrite As New FileStream(Dest, FileMode.Create)
Dim zipStream As New GZipStream(fsWrite, CompressionMode.Compress)
Dim Writer As New StreamWriter(zipStream)
Writer.Write(My.Computer.FileSystem.ReadAllBytes(source))
Writer.Flush()
zipStream.Flush()
fsWrite.Close()
End Sub
Private Sub UnZipFile(ByVal source As String, ByVal Dest As String)
Dim fsRead As New FileStream(source, FileMode.Open)
Dim UnZipStream As New GZipStream(fsRead, CompressionMode.Decompress)
Dim TotalByte As Long = fsRead.Length
Dim buff(TotalByte) As Byte
fsRead.Read(buff, 0, buff.Length)
fsRead.Close()
My.Computer.FileSystem.WriteAllBytes(Dest, buff, False)
End Sub
Private Sub mnZip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnZip.Click
Select Case ActiveView.FocusedItem.ImageIndex
Case FileSystemImageList.FolderClosedIconIndex, FileSystemImageList.FolderOpenIconIndex
MsgBox("Không mở được thư mục nén File")
Case Else
Dim sourceFile As String = ActiveView.FocusedItem.Tag
Dim DestFolder As String = LeftView.Tag
If ActiveView Is LeftView Then DestFolder = RightView.Tag
Dim DestFile As String = DestFolder & IO.Path.GetFileNameWithoutExtension(sourceFile)
DestFile = InputBox("Nhập tên File cần nén", "Zip", DestFile & ".zip")
If DestFile <> "" Then
ZipFile(sourceFile, DestFile)
LoadFolderAndFiles(RightView.Tag, RightView)
LoadFolderAndFiles(LeftView.Tag, LeftView)
End If
End Select
End Sub
Private Sub mnUnZip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnUnZip.Click
Select Case ActiveView.FocusedItem.ImageIndex
Case FileSystemImageList.FolderClosedIconIndex, FileSystemImageList.FolderOpenIconIndex
MsgBox("Không tìm thấy thư mục ")
Case Else
Dim sourceFile As String = ActiveView.FocusedItem.Tag
Dim DestFolder As String = LeftView.Tag
If ActiveView Is LeftView Then DestFolder = RightView.Tag
Dim DestFile As String = DestFolder & IO.Path.GetFileNameWithoutExtension(sourceFile)
DestFile = InputBox("Tên File cần giải nén", "UnZip", DestFile)
If DestFile <> "" Then
UnZipFile(sourceFile, DestFile)
LoadFolderAndFiles(RightView.Tag, RightView)
LoadFolderAndFiles(LeftView.Tag, LeftView)
End If
End Select
End Sub
Private Sub mnTree_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnTree.Click
TreeForm.RootFolder = ActiveView.Tag
TreeForm.FormParent = Me
TreeForm.Show()
End Sub
Private Sub FileAction(ByVal action As String)
Dim isSelectFolder As Boolean = False
Dim TargetView As ListView = LeftView
If ActiveView Is LeftView Then TargetView = RightView
Dim sourceFolder As String = ActiveView.Tag
Dim targetFolder As String = TargetView.Tag
Dim selectFolder As String = ""
Dim sourceFile As String = ""
Dim targetFile As String = ""
Try
For Each item As ListViewItem In ActiveView.SelectedItems
Select Case item.ImageIndex
Case FileSystemImageList.FolderClosedIconIndex
isSelectFolder = True
selectFolder = IO.Path.GetFileName(item.Tag)
Case Else
sourceFile = item.Tag
targetFile = targetFolder & "\" & IO.Path.GetFileName(sourceFile)
End Select
Select Case action
Case "Copy"
If isSelectFolder Then
My.Computer.FileSystem.CopyDirectory(sourceFolder & selectFolder, targetFolder & selectFolder)
Else
My.Computer.FileSystem.CopyFile(sourceFile, targetFile)
End If
Case "Rename"
Dim NewName As String
If isSelectFolder Then
NewName = InputBox("Nhập tên thư mục:","Rename", targetFolder & selectFolder)
If NewName <> "" Then
My.Computer.FileSystem.RenameDirectory(sourceFolder & selectFolder, NewName)
End If
Else
NewName = InputBox("Nhập tên thư mục:", "Rename", sourceFile)
If NewName <> "" Then
My.Computer.FileSystem.RenameFile(sourceFile, NewName)
End If
End If
Case "Move"
Dim NewName As String
If isSelectFolder Then
NewName = InputBox("Di chuyển tới:", "Move", targetFolder & selectFolder)
If NewName <> "" Then
My.Computer.FileSystem.MoveDirectory(sourceFolder & selectFolder, NewName)
End If
Else
NewName = InputBox("Di chuyển tới:", "Move", targetFile)
If NewName <> "" Then
My.Computer.FileSystem.RenameFile(sourceFile, NewName)
End If
End If
Case "Delete"
If isSelectFolder Then
If MsgBox("Xóa thư mục " & sourceFolder & selectFolder & "?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
My.Computer.FileSystem.DeleteDirectory(sourceFolder & selectFolder, FileIO.DeleteDirectoryOption.DeleteAllContents)
End If
Else
If MsgBox("Xóa File " & sourceFile & "?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
My.Computer.FileSystem.DeleteFile(sourceFile)
End If
End If
Case "CreateFolder"
Dim NewFolder As String = InputBox("Nhập tên thư mục cần tạo", "Create", "")
If NewFolder <> "" Then
My.Computer.FileSystem.CreateDirectory(sourceFolder & NewFolder)
End If
End Select
Next
LoadFolderAndFiles(ActiveView.Tag, ActiveView)
LoadFolderAndFiles(TargetView.Tag, TargetView)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopy.Click
FileAction("Copy")
End Sub
Private Sub btnMove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMove.Click
FileAction("Move")
End Sub
Private Sub btnNewFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewFolder.Click
FileAction("CreateFolder")
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
FileAction("Delete")
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Application.Exit()
End Sub
Private Sub RightView_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles RightView.ColumnClick
LoadFolderAndFiles(RightView.Tag, LeftView, e.Column)
End Sub
Private Sub help_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles help.Click
MsgBox("Chương trình chỉ mang tính chất chứng minh VB.Net đủ mạnh", MsgBoxStyle.Critical, "Visual Basic.Net rất mạnh")
End Sub
End Class
- Để chương trình trên chạy được bạn cần thêm modul sau vào chương trình mang tên FileSystemImageList.VB mã nguồn modul:
Option Explicit On
Imports System
Imports System.Runtime.InteropServices
Friend Class StatusUpdate
Inherits System.ComponentModel.Component
Private Const CLASS_STATUSUPDATE As String = "StatusUpdate"
Private theStatusBar As StatusBar
Public Sub New( _
ByVal container As System.ComponentModel.IContainer, _
ByVal status As StatusBar)
container.Add(Me, CLASS_STATUSUPDATE)
theStatusBar = status
End Sub
Public Sub New( _
ByVal container As System.ComponentModel.IContainer)
container.Add(Me, CLASS_STATUSUPDATE)
theStatusBar = Nothing
End Sub
Public Property [StatusBar]() As StatusBar
Get
Return theStatusBar
End Get
Set(ByVal Value As StatusBar)
theStatusBar = Value
End Set
End Property
Public Sub Update(ByVal updateText As String)
If Not (StatusBar Is Nothing) Then
theStatusBar.Text = updateText
End If
End Sub
End Class
Public Class FileSystemImageList
Private theShell32 As New Shell32
Public Const FolderClosedIconIndex As Integer = 0
Public Const FolderOpenIconIndex As Integer = 1
Public Const FileGenericIconIndex As Integer = 2
Private Const CLASS_FILESYSTEMIMAGELIST As String = _
"FileSystemImageList"
Private theExtensionIconIndex As Hashtable
Private theSmallImageList As ImageList
Private theLargeImageList As ImageList
Public Sub New( _
ByVal container As System.ComponentModel.IContainer)
container.Add(Me, CLASS_FILESYSTEMIMAGELIST)
InitializeComponent()
End Sub
Public Sub New()
InitializeComponent()
End Sub
Sub InitializeComponent()
theSmallImageList = New ImageList
theSmallImageList.ColorDepth = _
System.Windows.Forms.ColorDepth.Depth8Bit
theSmallImageList.ImageSize = _
New System.Drawing.Size(16, 16)
theSmallImageList.TransparentColor = _
System.Drawing.Color.Transparent
theLargeImageList = New ImageList
theLargeImageList.ColorDepth = _
System.Windows.Forms.ColorDepth.Depth8Bit
theLargeImageList.ImageSize = _
New System.Drawing.Size(64, 64)
theLargeImageList.TransparentColor = _
System.Drawing.Color.Transparent
theExtensionIconIndex = _
New System.Collections.Hashtable
AddFolderIcon(Convert.ToUInt32(0))
AddFolderIcon(Convert.ToUInt32(Shell32.SHGFI_OPENICON))
AddFileIcon(Nothing)
End Sub
Public ReadOnly Property SmallImageList() As ImageList
Get
Return theSmallImageList
End Get
End Property
Public ReadOnly Property LargeImageList() As ImageList
Get
Return theLargeImageList
End Get
End Property
Private Sub AddFolderIcon( _
ByVal flags As System.UInt32)
flags = Convert.ToUInt32( _
Convert.ToInt32(flags) Or _
Convert.ToInt32(Shell32.SHGFI_ICON) Or _
Convert.ToInt32(Shell32.SHGFI_USEFILEATTRIBUTES))
Dim shfi As New Shell32.SHFILEINFO
Shell32.SHGetFileInfo( _
Nothing, _
Shell32.FILE_ATTRIBUTE_DIRECTORY, _
shfi, _
Convert.ToUInt32( _
System.Runtime.InteropServices.Marshal.SizeOf(shfi)), _
flags)
Dim icon As System.Drawing.Icon = _
System.Drawing.Icon.FromHandle(shfi.hIcon)
theLargeImageList.Images.Add(icon)
User32.DestroyIcon(shfi.hIcon)
flags = Convert.ToUInt32(Convert.ToInt32(flags) Or _
Convert.ToInt32(Shell32.SHGFI_SMALLICON))
Shell32.SHGetFileInfo( _
Nothing, _
Shell32.FILE_ATTRIBUTE_DIRECTORY, _
shfi, _
Convert.ToUInt32( _
System.Runtime.InteropServices.Marshal.SizeOf(shfi)), _
flags)
icon = System.Drawing.Icon.FromHandle(shfi.hIcon)
theSmallImageList.Images.Add(icon)
User32.DestroyIcon(shfi.hIcon)
End Sub
Private Function AddFileIcon(ByVal name As String) As Integer
Dim shfi As New Shell32.SHFILEINFO
Shell32.SHGetFileInfo( _
name, _
Shell32.FILE_ATTRIBUTE_NORMAL, _
shfi, _
System.Convert.ToUInt32( _
Runtime.InteropServices.Marshal.SizeOf(shfi)), _
Convert.ToUInt32( _
Convert.ToInt32( _
Shell32.SHGFI_USEFILEATTRIBUTES) Or _
Convert.ToInt32(Shell32.SHGFI_ICON)))
Dim icon As System.Drawing.Icon = _
System.Drawing.Icon.FromHandle(shfi.hIcon)
theLargeImageList.Images.Add(icon)
User32.DestroyIcon(shfi.hIcon)
shfi = New Shell32.SHFILEINFO
Shell32.SHGetFileInfo( _
name, _
Shell32.FILE_ATTRIBUTE_NORMAL, _
shfi, _
Convert.ToUInt32( _
System.Runtime.InteropServices.Marshal.SizeOf(shfi)), _
Convert.ToUInt32( _
Convert.ToInt32(Shell32.SHGFI_ICON) Or _
Convert.ToInt32(Shell32.SHGFI_SMALLICON) Or _
Convert.ToInt32(Shell32.SHGFI_USEFILEATTRIBUTES)))
icon = System.Drawing.Icon.FromHandle(shfi.hIcon)
theSmallImageList.Images.Add(icon)
User32.DestroyIcon(shfi.hIcon)
If Not (name Is Nothing) Then
Dim iconindex As Integer = _
SmallImageList.Images.Count - 1
theExtensionIconIndex.Add(name, iconindex)
Return iconindex
End If
Return FileGenericIconIndex
End Function
Public Function GetTheFileIconIndex( _
ByVal filename As String) As Integer
Dim index As Integer = filename.LastIndexOf(".")
If index > -1 Then
Dim extension As String = filename.Substring(index)
If Not theExtensionIconIndex.ContainsKey(extension) Then
Return AddFileIcon(extension)
Else
Return CInt(theExtensionIconIndex(extension))
End If
Else
Return FileGenericIconIndex
End If
End Function
End Class
Public Class Shell32
Public Const MAX_PATH As Integer = 256
<StructLayout(LayoutKind.Sequential)> _
Public Structure SHITEMID
<MarshalAs(UnmanagedType.LPArray)> _
Public cb As System.UInt16
Public abID() As Byte
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure ITEMIDLIST
Public mkid As SHITEMID
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure BROWSEINFO
Public hwndOwner As IntPtr
Public pidlRoot As IntPtr
Public pszDisplayName As IntPtr
<MarshalAs(UnmanagedType.LPTStr)> _
Public lpszTitle As String
Public ulFlags As System.UInt32
Public lpfn As IntPtr
Public lParam As Integer
Public iImage As IntPtr
End Structure
Public Shared BIF_RETURNONLYFSDIRS As System.UInt32 = _
Convert.ToUInt32(&H1)
Public Shared BIF_DONTGOBELOWDOMAIN As System.UInt32 = _
Convert.ToUInt32(&H2)
Public Shared BIF_STATUSTEXT As System.UInt32 = _
Convert.ToUInt32(&H4)
Public Shared BIF_RETURNFSANCESTORS As System.UInt32 = _
Convert.ToUInt32(&H8)
Public Shared BIF_EDITBOX As System.UInt32 = _
Convert.ToUInt32(&H10)
Public Shared BIF_VALIDATE As System.UInt32 = _
Convert.ToUInt32(&H20)
Public Shared BIF_NEWDIALOGSTYLE As System.UInt32 = _
Convert.ToUInt32(&H40)
Public Shared BIF_USENEWUI As System.UInt32 = _
Convert.ToUInt32(&H50)
Public Shared BIF_BROWSEINCLUDEURLS As System.UInt32 = _
Convert.ToUInt32(&H80)
Public Shared BIF_BROWSEFORCOMPUTER As System.UInt32 = _
Convert.ToUInt32(&H1000)
Public Shared BIF_BROWSEFORPRINTER As System.UInt32 = _
Convert.ToUInt32(&H2000)
Public Shared BIF_BROWSEINCLUDEFILES As System.UInt32 = _
Convert.ToUInt32(&H4000)
Public Shared BIF_SHAREABLE As System.UInt32 = _
Convert.ToUInt32(&H8000)
Public szDisplayName As String
<StructLayout(LayoutKind.Sequential)> _
Public Structure SHFILEINFO
Public hIcon As IntPtr
Public iIcon As Integer
Public dwAttributes As System.UInt32
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
Public szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
Public szTypeName As String
End Structure
Public Shared SHGFI_ICON As System.UInt32 = _
Convert.ToUInt32(&H100)
Public Shared SHGFI_DISPLAYNAME As System.UInt32 = _
Convert.ToUInt32(&H200)
Public Shared SHGFI_TYPENAME As System.UInt32 = _
Convert.ToUInt32(&H400)
Public Shared SHGFI_ATTRIBUTES As System.UInt32 = _
Convert.ToUInt32(&H800)
Public Shared SHGFI_ICONLOCATION As System.UInt32 = _
Convert.ToUInt32(&H1000)
Public Shared SHGFI_EXETYPE As System.UInt32 = _
Convert.ToUInt32(&H2000)
Public Shared SHGFI_SYSICONINDEX As System.UInt32 = _
Convert.ToUInt32(&H4000)
Public Shared SHGFI_LINKOVERLAY As System.UInt32 = _
Convert.ToUInt32(&H8000)
Public Shared SHGFI_SELECTED As System.UInt32 = _
Convert.ToUInt32(&H10000)
Public Shared SHGFI_ATTR_SPECIFIED As System.UInt32 = _
Convert.ToUInt32(&H20000)
Public Shared SHGFI_LARGEICON As System.UInt32 = _
Convert.ToUInt32(&H0)
Public Shared SHGFI_SMALLICON As System.UInt32 = _
Convert.ToUInt32(&H1)
Public Shared SHGFI_OPENICON As System.UInt32 = _
Convert.ToUInt32(&H2)
Public Shared SHGFI_SHELLICONSIZE As System.UInt32 = _
Convert.ToUInt32(&H4)
Public Shared SHGFI_PIDL As System.UInt32 = _
Convert.ToUInt32(&H8)
Public Shared SHGFI_USEFILEATTRIBUTES As System.UInt32 = _
Convert.ToUInt32(&H10)
Public Shared SHGFI_ADDOVERLAYS As System.UInt32 = _
Convert.ToUInt32(&H20)
Public Shared SHGFI_OVERLAYINDEX As System.UInt32 = _
Convert.ToUInt32(&H40)
Public Shared FILE_ATTRIBUTE_DIRECTORY As System.UInt32 = _
Convert.ToUInt32(&H10)
Public Shared FILE_ATTRIBUTE_NORMAL As System.UInt32 = _
Convert.ToUInt32(&H80)
<DllImport("Shell32.dll")> _
Public Shared Function SHGetFileInfo( _
ByVal pszPath As String, _
ByVal dwFileAttributes As System.UInt32, _
ByRef psfi As SHFILEINFO, _
ByVal cbFileInfo As System.UInt32, _
ByVal uFlags As System.UInt32) As IntPtr
End Function
<DllImport("Shell32.dll")> _
Public Shared Function SHBrowseForFolder( _
ByRef lpbi As BROWSEINFO) As IntPtr
End Function
<DllImport("Shell32.dll")> _
Public Shared Function SHGetPathFromIDList( _
ByVal pidl As IntPtr, _
ByVal pszPath As IntPtr) As System.UInt32
End Function
End Class
Public Class User32
<DllImport("User32.dll")> _
Public Shared Function DestroyIcon( _
ByVal hIcon As IntPtr) As Integer
End Function
End Class
Co van de gi can giai dap.
OK.