Embedding source code in a source program
- Option Strict On
- Imports System.ComponentModel
- Imports System.Runtime.InteropServices
- Imports System.Text
- Imports Shell32
- Public Class Form1
- Dim Shell_Application As Shell32.Shell
- Dim SelectedPath As String
- Dim DateTimeFormat As String
- Dim Time_Interval As TimeSpan
- Dim MyTaskFactory As TaskFactory
- Dim IsBusyFindingFiles As Boolean
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- Dim RectangleConverter As New RectangleConverter()
- Dim a As Rectangle =
- CType(RectangleConverter.ConvertFromString(My.Settings.Form1Position), Rectangle)
- Tracer.Append($"{Me.StartPosition} {Me.WindowState}
- {Me.Location} {Me.Size}
- {a.Location} {a.Size}")
- Me.Location = a.Location
- Me.Size = a.Size
- Me.WindowState = CType(
- Int32.Parse(My.Settings.WindowState), FormWindowState)
- hWnd_Form1 = Me.Handle
- CheckBox1.Checked = Bool_Find_Filename
- CheckBox2.Checked = Bool_Modified_Files
- CheckBox3.Checked = Bool_Like_Filename
- TextBox1.Text = FileNameToBeFound
- TextBox2.Text = Like_FileNameToBeFound
- Label2.Text = SelectedPath
- Me.Button1.Focus()
- End Sub
- Sub Initialize()
- Try
- Me.Button1.Text = My.Settings1.Default.Button1Text
- Me.Button2.Text = My.Settings1.Default.Button2Text
- Me.CheckBox1.Text = My.Settings1.Default.CheckBox1Text
- Me.CheckBox2.Text = My.Settings1.Default.CheckBox2Text
- Me.CheckBox3.Text = My.Settings1.Default.CheckBox3Text
- Me.DateTimeFormat = My.Settings1.Default.DateTimeFormat
- Me.Text = My.Settings1.Default.Form1Text
- SelectedPath = My.Settings.SelectedPath
- Like_FileNameToBeFound = My.Settings.Like_FilenameToBeFound
- FileNameToBeFound = My.Settings.FilenameToBeFound
- Dim BoolConverter As New BooleanConverter()
- Bool_Find_Filename = CType(BoolConverter.ConvertFromString(
- My.Settings.Bool_Find_Filename), Boolean)
- Bool_Like_Filename = CType(BoolConverter.ConvertFromString(
- My.Settings.Bool_Like_Filename), Boolean)
- Bool_Modified_Files = CType(BoolConverter.ConvertFromString(
- My.Settings.Bool_Modified_Files), Boolean)
- Catch ex As Exception
- End Try
- Dim Time_Now As DateTime = DateTime.Now
- Time_UpLimitation = Time_Now.AddMinutes(-1)
- Time_DownLimitation = Time_Now
- With DateTimePicker1
- .Format = DateTimePickerFormat.Custom
- .CustomFormat = DateTimeFormat
- .Value = DateTime.Parse(Time_UpLimitation.ToString(DateTimeFormat))
- End With
- With DateTimePicker2
- .Format = DateTimePickerFormat.Custom
- .CustomFormat = DateTimeFormat
- .Value = DateTime.Parse(Time_DownLimitation.ToString(DateTimeFormat))
- End With
- Set_Time_Interval()
- End Sub
- Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
- Dim RectangleConverter As New RectangleConverter()
- Dim WindowState2 As FormWindowState = Me.WindowState
- Dim Rectange2 As New Rectangle(Me.Location, Me.Size)
- If Me.WindowState <> FormWindowState.Normal Then
- WindowState2 = PreviousWindowState
- Rectange2 = Me.RestoreBounds
- End If
- My.Settings.WindowState = CInt(WindowState2).ToString()
- My.Settings.Form1Position = RectangleConverter.ConvertToString(Rectange2)
- My.Settings.SelectedPath = SelectedPath
- My.Settings.Like_FilenameToBeFound = Like_FileNameToBeFound
- My.Settings.FilenameToBeFound = FileNameToBeFound
- Dim BoolConverter As New BooleanConverter()
- My.Settings.Bool_Find_Filename = BoolConverter.ConvertToString(Bool_Find_Filename)
- My.Settings.Bool_Like_Filename = BoolConverter.ConvertToString(Bool_Like_Filename)
- My.Settings.Bool_Modified_Files = BoolConverter.ConvertToString(Bool_Modified_Files)
- End Sub
- Dim PreviousWindowState As FormWindowState
- Protected Overrides Sub WndProc(ByRef m As Message)
- Static BackColor_Button1 As Color = Me.Button1.BackColor
- Select Case m.Msg
- Case WM_MyMessage
- If m.WParam = IntPtr.Zero Then
- Me.IsBusyFindingFiles = False
- Me.Button1.BackColor = BackColor_Button1
- ElseIf m.WParam = New IntPtr(1) Then
- Me.Button1.BackColor = Color.DarkGreen
- Else
- MsgBox($"{m.WParam.ToInt32}", Title:="WM_MyMessage")
- End If
- m.Result = IntPtr.Zero
- Exit Sub
- Case WM_SIZE
- If m.WParam.ToInt32 = SIZE_MAXIMIZED Then
- PreviousWindowState = FormWindowState.Maximized
- ElseIf m.WParam.ToInt32 = SIZE_RESTORED Then
- PreviousWindowState = FormWindowState.Normal
- End If
- End Select
- MyBase.WndProc(m)
- End Sub
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Static FirstTime As Boolean = True
- If IsBusyFindingFiles Then Exit Sub
- If FirstTime Then
- FirstTime = False
- Shell_Application = New Shell32.Shell()
- MyTaskFactory = New TaskFactory(
- TaskCreationOptions.PreferFairness Or
- TaskCreationOptions.LongRunning, Nothing)
- End If
- Dim SelectedFolder As Folder = CType(Shell_Application.NameSpace(SelectedPath), Folder)
- Dim FindFiles As New Find_Files_Class
- Button1.BackColor = Color.Red
- MyTaskFactory.StartNew(AddressOf FindFiles.FindFiles_and_ShowResult, SelectedFolder)
- End Sub
- Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) _
- Handles DateTimePicker1.ValueChanged, DateTimePicker2.ValueChanged
- Time_UpLimitation = DateTimePicker1.Value
- Time_DownLimitation = DateTimePicker2.Value
- Set_Time_Interval()
- End Sub
- Sub Set_Time_Interval()
- Static BackColor_Label1 As Color = Label1.BackColor
- Time_Interval = DateTimePicker2.Value - DateTimePicker1.Value
- Label1.Text = $"{Time_Interval,18:c}"
- Label1.BackColor = If(Time_Interval <= TimeSpan.Zero, Color.Red, BackColor_Label1)
- End Sub
- Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
- Static FirstTime_Folder As Boolean = True
- If FirstTime_Folder Then
- FirstTime_Folder = False
- FolderBrowserDialog1.SelectedPath = SelectedPath
- FolderBrowserDialog1.ShowNewFolderButton = False
- End If
- If DialogResult.OK = FolderBrowserDialog1.ShowDialog(New HWND_IWin32(Me.Handle)) Then
- SelectedPath = FolderBrowserDialog1.SelectedPath
- Label2.Text = SelectedPath
- End If
- Button1.Focus()
- End Sub
- Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
- Bool_Find_Filename = CheckBox1.Checked
- End Sub
- Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
- Bool_Modified_Files = CheckBox2.Checked
- End Sub
- Private Sub CheckBox3_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox3.CheckedChanged
- Bool_Like_Filename = CheckBox3.Checked
- End Sub
- Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
- FileNameToBeFound = TextBox1.Text
- End Sub
- Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
- Like_FileNameToBeFound = TextBox2.Text
- End Sub
- Dim Tracer As New StringBuilder
- Private Sub Form1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles Me.MouseDoubleClick
- MsgBox(Tracer.ToString())
- End Sub
- Private Sub Form1_Layout(sender As Object, e As LayoutEventArgs) Handles Me.Layout
- Static FirstTime As Boolean = True
- If FirstTime Then
- FirstTime = False
- Me.Initialize()
- End If
- End Sub
- End Class
- Module Module1
- Public Null As IntPtr = IntPtr.Zero
- Public hWnd_Form1 As IntPtr
- Public Time_UpLimitation As DateTime
- Public Time_DownLimitation As DateTime
- Public Bool_Find_Filename As Boolean
- Public Bool_Like_Filename As Boolean
- Public Bool_Modified_Files As Boolean
- Public FileNameToBeFound As String
- Public Like_FileNameToBeFound As String
- Class HWND_IWin32
- Implements IWin32Window
- Dim _Handle As IntPtr
- Sub New(hWnd As IntPtr)
- _Handle = hWnd
- End Sub
- Public ReadOnly Property Handle As IntPtr Implements IWin32Window.Handle
- Get
- Return _Handle
- End Get
- End Property
- End Class
- Function GetProcessId_from_hWnd(hWnd As IntPtr) As UInt32
- Dim ProcessId As UInt32
- Dim ThreadId As UInt32
- If GetWindow(hWnd, GW_OWNER) = Null AndAlso IsWindowVisible(hWnd) Then
- ThreadId = GetWindowThreadProcessId(hWnd, ProcessId)
- End If
- Return ProcessId
- End Function
- Public Const FORMAT_MESSAGE_FROM_SYSTEM As UInteger = &H1000
- Public Const WM_MOVE As UInt32 = &H3
- Public Const WM_SIZE As UInt32 = &H5
- Public Const SIZE_RESTORED As Integer = 0
- Public Const SIZE_MINIMIZED As Integer = 1
- Public Const SIZE_MAXIMIZED As Integer = 2
- Public Const SIZE_MAXSHOW As Integer = 3
- Public Const SIZE_MAXHIDE As Integer = 4
- Public Const WM_SETFOCUS As UInt32 = &H7
- Public Const WM_KILLFOCUS As UInt32 = &H8
- Public Const WM_ENABLE As UInt32 = &HA
- Public Const WM_SETREDRAW As UInt32 = &HB
- Public Const WM_SETTEXT As UInt32 = &HC
- Public Const WM_GETTEXT As UInt32 = &HD
- Public Const WM_GETTEXTLENGTH As UInt32 = &HE
- Public Const WM_PAINT As UInt32 = &HF
- Public Const WM_CLOSE As UInt32 = &H10
- Public Const WM_QUERYOPEN As UInt32 = &H13
- Public Const WM_QUIT As UInt32 = &H12
- Public Const WM_COMMAND As UInt32 = &H111
- Public Const WM_SYSCOMMAND As UInt32 = &H112
- Public Const WM_APP As UInt32 = &H8000
- Public Const WM_USER As UInt32 = &H400
- Public Const WM_MyMessage As UInt32 = WM_APP
- Public Const MAX_PATH As Int32 = 260
- Enum Flag_BIF
- BIF_RETURNONLYFSDIRS = &H1 ' For finding a folder to start document searching
- BIF_DONTGOBELOWDOMAIN = &H2 ' For starting the Find Computer
- BIF_STATUSTEXT = &H4 ' Top Of the dialog has 2 lines Of text For BROWSEINFO.lpszTitle And one line If
- ' this flag Is set. Passing the message BFFM_SETSTATUSTEXTA to the hwnd can set the
- ' // rest of the text. This Is Not used with BIF_USENEWUI And BROWSEINFO.lpszTitle gets
- ' // all three lines of text.
- BIF_RETURNFSANCESTORS = &H8
- BIF_EDITBOX = &H10 '// Add an editbox To the dialog
- BIF_VALIDATE = &H20 '// insist on valid result (Or CANCEL)
- BIF_NEWDIALOGSTYLE = &H40 '// Use the New dialog layout with the ability to resize
- '// Caller needs to call OleInitialize() before using this API
- BIF_USENEWUI = (BIF_NEWDIALOGSTYLE Or BIF_EDITBOX)
- BIF_BROWSEINCLUDEURLS = &H80 '// Allow URLs To be displayed Or entered. (Requires BIF_USENEWUI)
- BIF_UAHINT = &H100 '// Add a UA hint To the dialog, In place Of the edit box. May Not be combined With BIF_EDITBOX
- BIF_NONEWFOLDERBUTTON = &H200 '// Do Not add the "New Folder" button To the dialog. Only applicable With BIF_NEWDIALOGSTYLE.
- BIF_NOTRANSLATETARGETS = &H400 '// don 't traverse target as shortcut
- BIF_BROWSEFORCOMPUTER = &H1000 '// Browsing For Computers.
- BIF_BROWSEFORPRINTER = &H2000 '// Browsing For Printers
- BIF_BROWSEINCLUDEFILES = &H4000 '// Browsing For Everything
- BIF_SHAREABLE = &H8000 '// sharable resources displayed (remote shares, requires BIF_USENEWUI)
- BIF_BROWSEFILEJUNCTIONS = &H10000 '// allow folder junctions Like zip files And libraries To be browsed
- End Enum
- Public GW_HWNDFIRST As UInt32 = 0
- Public GW_HWNDLAST As UInt32 = 1
- Public GW_HWNDNEXT As UInt32 = 2
- Public GW_HWNDPREV As UInt32 = 3
- Public GW_OWNER As UInt32 = 4
- Public GW_CHILD As UInt32 = 5
- Public GW_ENABLEDPOPUP As UInt32 = 6
- Public GW_MAX As UInt32 = 6
- Public Const S_OK As Int32 = 0
- Public Const S_FALSE As Int32 = 1I
- Public Const PMSF_NORMAL As UInt32 = 0
- Public Const PMSF_MULTIPLE As UInt32 = 1
- Public Const PMSF_DONT_STRIP_SPACES As UInt32 = &H10000
- Declare Unicode Function PathMatchSpecEx Lib "Shlwapi.dll" Alias "PathMatchSpecExW" (
- _In_LPCWSTR_pszFile As String,
- _In_LPCWSTR_pszSpec As String,
- _In_DWORD_dwFlags As UInt32) As Int32
- Declare Unicode Function PathIsDirectory Lib "Shlwapi.dll" Alias "PathIsDirectoryW" (
- _In_LPCWSTR_pszPath As String) As Boolean
- Declare Unicode Function PathFileExists Lib "Shlwapi.dll" Alias "PathFileExistsW" (
- _In_PCWSTR_pszPath As String) As Boolean
- Declare Unicode Function FormatMessage Lib "kernel32.dll" Alias "FormatMessageW" (
- _In_DWORD_dwFlags As UInt32,
- _In_opt_LPCVOID_lpSource As IntPtr,
- _In_DWORD_dwMessageId As UInt32,
- _In_DWORD_dwLanguageId As UInt32,
- _Out_LPWSTR_lpBuffer As IntPtr,
- _In_DWORD_nSize As UInt32,
- _In_opt_va_list_Arguments As IntPtr) As UInt32
- Declare Unicode Function GetLastError Lib "kernel32.dll" () As UInt32
- Declare Unicode Function GetModuleFileName Lib "Kernel32.dll" Alias "GetModuleFileNameW" (
- _In_opt_hModule As IntPtr,
- lpFilename As IntPtr,
- nSize As UInt32) As UInt32
- Declare Unicode Function GetModuleFileName Lib "Kernel32.dll" Alias "GetModuleFileNameW" (
- <InAttribute> _In_opt_hModule As IntPtr,
- <InAttribute, Out> lpFilename As StringBuilder,
- <InAttribute> nSize As UInt32) As UInt32
- Declare Unicode Function CloseHandle Lib "kernel32.dll" (
- ByVal hFile As IntPtr) As Boolean
- Declare Unicode Function DestroyWindow Lib "User32.dll" (
- _In_HWND_hWnd As IntPtr) As Boolean
- Declare Unicode Function CloseWindow Lib "User32" (
- _In_HWND_hWnd As IntPtr) As Boolean
- Declare Unicode Function FindWindow Lib "User32.dll" Alias "FindWindowW" (
- _In_opt__LPCWSTR_lpClassName As String,
- _In_opt__LPCWSTR_lpWindowName As String) As IntPtr
- Declare Unicode Function EnableWindow Lib "User32.dll" (
- _In_HWND_hWnd As IntPtr,
- _In_BOOL_bEnable As Boolean) As Boolean
- Declare Unicode Function IsWindowEnabled Lib "User32.dll" (
- _In_HWND_hWnd As IntPtr) As Boolean
- Declare Unicode Function BringWindowToTop Lib "User32.dll" (
- _In_HWND_hWnd As IntPtr) As Boolean
- Declare Unicode Function GetForegroundWindow Lib "User32.dll" () As IntPtr
- Declare Unicode Function SetForegroundWindow Lib "User32.dll" (
- _In_HWND_hWnd As IntPtr) As Boolean
- Declare Unicode Function PostMessage Lib "user32.dll" Alias "PostMessageW" (
- _In_opt_HWND_hWnd As IntPtr,
- _In__UINT_Msg As UInt32,
- _In_WPARAM_wParam As IntPtr,
- _In_LPARAM_lParam As IntPtr) As Boolean
- Declare Unicode Function SetActiveWindow Lib "User32.dll" (_In_HWND_hWnd As IntPtr) As IntPtr
- Declare Unicode Function SetFocus Lib "User32.dll" (
- _In_opt_HWND_hWnd As IntPtr) As IntPtr
- Declare Unicode Function GetWindow Lib "User32.dll" (
- _In_HWND_hWnd As IntPtr,
- _In_UINT_uCmd As UInt32) As IntPtr
- Declare Unicode Function GetDesktopWindow Lib "User32.dll" () As IntPtr
- Declare Unicode Function GetWindowThreadProcessId Lib "User32.dll" (
- _In_HWND_hWnd As IntPtr,
- _Out_opt_LPDWORD_lpdwProcessId As IntPtr) As UInt32
- Declare Unicode Function GetWindowThreadProcessId Lib "User32.dll" (
- _In_HWND_hWnd As IntPtr,
- <InAttribute, Out> ByRef _Out_opt_LPDWORD_lpdwProcessId As UInt32) As UInt32
- Declare Unicode Function IsWindowVisible Lib "user32.dll" (
- _In_HWND_hWnd As IntPtr) As Boolean
- Declare Unicode Function GetWindowText Lib "User32.dll" Alias "GetWindowTextW" (
- _In_HWND_hWnd As IntPtr,
- _Out_writes_nMaxCount_LPWSTR_lpString As IntPtr,
- _In_int_nMaxCount As Int32) As Int32 ' _Ret_range_(0, nMaxCount)
- Declare Unicode Function SendMessage Lib "user32.dll" Alias "SendMessageW" (
- _In__HWND_hWnd As IntPtr,
- _In__UINT_Msg As UInt32,
- _Pre_maybenull__Post_valid__WPARAM_wParam As IntPtr,
- _Pre_maybenull___Post_valid__LPARAM_lParam As IntPtr) As IntPtr
- End Module
- Imports Shell32
- Public Class Find_Files_Class
- Public _Time_UpLimitation As DateTime
- Public _Time_DownLimitation As DateTime
- Public _Bool_Find_Filename As Boolean
- Public _Bool_Like_Filename As Boolean
- Public _Bool_Modified_Files As Boolean
- Public _FileNameToBeFound As String
- Public _Like_FileNameToBeFound As String
- Public ModifiedFolderItems As System.Collections.Generic.List(Of FolderItem)
- Public FoundFilenames As Collections.Generic.List(Of FolderItem)
- Public Like_Filenames_BeFound As Collections.Generic.List(Of FolderItem)
- Public Sub New()
- _Time_UpLimitation = Time_UpLimitation
- _Time_DownLimitation = Time_DownLimitation
- _Bool_Find_Filename = Bool_Find_Filename
- _Bool_Like_Filename = Bool_Like_Filename
- _Bool_Modified_Files = Bool_Modified_Files
- _FileNameToBeFound = FileNameToBeFound
- _Like_FileNameToBeFound = Like_FileNameToBeFound
- ModifiedFolderItems = New System.Collections.Generic.List(Of FolderItem)
- FoundFilenames = New Collections.Generic.List(Of FolderItem)
- Like_Filenames_BeFound = New Collections.Generic.List(Of FolderItem)
- End Sub
- Sub clear()
- ModifiedFolderItems.Clear()
- FoundFilenames.Clear()
- Like_Filenames_BeFound.Clear()
- End Sub
- Protected Overrides Sub Finalize()
- clear()
- End Sub
- Sub FindFiles_and_ShowResult(SelectedFolder2 As Object)
- PathIsNotDirectory = New List(Of String)
- PathFile_NotExists = New List(Of String)
- Dim SelectedFolder As Folder = CType(SelectedFolder2, Folder)
- Dim Title_SelectedFolder As String = SelectedFolder.Title
- FindFiles(SelectedFolder)
- PostMessage(hWnd_Form1, WM_MyMessage, New IntPtr(1), Null)
- Dim Excel_Outputl As New Excel_Output_Class(Me)
- If _Bool_Find_Filename Then Excel_Outputl.Show_FoundFilename(Title_SelectedFolder)
- If _Bool_Like_Filename Then Excel_Outputl.Show_Like_Filename_BeFound(Title_SelectedFolder)
- If _Bool_Modified_Files Then Excel_Outputl.Show_ModifyFolderItem(Title_SelectedFolder)
- Excel_Outputl.Common_ShowWOrkSheet()
- PostMessage(hWnd_Form1, WM_MyMessage, Null, Null)
- End Sub
- Dim PathIsNotDirectory As List(Of String)
- Dim PathFile_NotExists As List(Of String)
- Sub FindFiles(InputFolder As Folder)
- For Each Folder_Item As FolderItem In InputFolder.Items
- If PathFileExists(Folder_Item.Path) Then
- If Folder_Item.IsFolder Then
- If Not PathIsDirectory(Folder_Item.Path) Then
- PathIsNotDirectory.Add(Folder_Item.Path)
- Continue For
- End If
- FindFiles(DirectCast(Folder_Item.GetFolder, Folder))
- Else
- FiltersCollection(Folder_Item)
- End If
- Else
- PathFile_NotExists.Add(Folder_Item.Path)
- End If
- Next
- End Sub
- Function Filter_Modified_Files(Folder_item As FolderItem) As Boolean
- Return _Bool_Modified_Files AndAlso
- _Time_UpLimitation < Folder_item.ModifyDate AndAlso
- Folder_item.ModifyDate < _Time_DownLimitation
- End Function
- Function Filter_Find_FileName(Folder_item As FolderItem) As Boolean
- Return _Bool_Find_Filename AndAlso
- 0 = String.Compare(IO.Path.GetFileName(Folder_item.Path), _FileNameToBeFound, ignoreCase:=True)
- End Function
- Function Filter_Like_FileName(Folder_item As FolderItem) As Boolean
- Return _Bool_Like_Filename AndAlso
- S_OK = PathMatchSpecEx(Folder_item.Path, _Like_FileNameToBeFound, PMSF_DONT_STRIP_SPACES)
- End Function
- Public Sub FiltersCollection(Folder_Item As FolderItem)
- If Filter_Modified_Files(Folder_Item) Then ModifiedFolderItems.Add(Folder_Item)
- If Filter_Find_FileName(Folder_Item) Then FoundFilenames.Add(Folder_Item)
- If Filter_Like_FileName(Folder_Item) Then Like_Filenames_BeFound.Add(Folder_Item)
- MyEmbedProc(Folder_Item)
- End Sub
- Partial Private Sub MyEmbedProc(Folder_item As FolderItem)
- End Sub
- End Class
- Option Strict On
- Imports System.Runtime.InteropServices
- Imports Microsoft.Office.Interop
- Imports Microsoft.Office.Interop.Excel
- Imports Shell32
- Public Class Excel_Output_Class
- Public Excel_Application As Excel.Application
- Dim Workbook As Excel.Workbook
- Dim Worksheet As Excel.Worksheet
- Dim NumberOf_Added_Worksheets As Int32
- Dim cell As Range
- Dim Find_files As Find_Files_Class
- Dim Delete_Excel_Application As Boolean
- Public Sub New(Find_files As Find_Files_Class)
- Me.Find_files = Find_files
- Delete_Excel_Application = False
- Excel_Application = CType(CreateObject("Excel.Application"), Excel.Application)
- Excel_Application.DisplayAlerts = False
- AddHandler Excel_Application.SheetBeforeDoubleClick, AddressOf SheetBeforeDoubleClickEventHandler
- AddHandler Excel_Application.WorkbookBeforeClose, AddressOf WorkbookBeforeCloseEventHandler
- Workbook = Excel_Application.Workbooks.Add()
- Workbook.Windows(1).Caption = "Find files"
- 'Workbook.Title = "My Workbook Title"
- NumberOf_Added_Worksheets = 0
- End Sub
- '<TypeLibType(TypeLibTypeFlags.FHidden), ComVisible(False)>
- <ComVisible(False)>
- Public Delegate Sub AppEvents_SheetBeforeDoubleClickEventHandler(
- <InAttribute(), MarshalAs(UnmanagedType.IDispatch)> Sh As Object,
- <InAttribute(), MarshalAs(UnmanagedType.Interface)> Target As Range,
- <InAttribute(), Out> ByRef Cancel As Boolean)
- '<TypeLibType(TypeLibTypeFlags.FHidden), ComVisible(False)>
- <ComVisible(False)>
- Sub SheetBeforeDoubleClickEventHandler(
- <InAttribute(), MarshalAs(UnmanagedType.IDispatch)> Sh As Object,
- <InAttribute(), MarshalAs(UnmanagedType.Interface)> Target As Range,
- <InAttribute(), Out> ByRef Cancel As Boolean)
- If Target.Column <> 6 Then Exit Sub
- Dim WorkSheet As Worksheet = DirectCast(Sh, Worksheet)
- Dim Directory_string As String = TryCast(Target.Value2, String)
- If Not PathIsDirectory(Directory_string) Then Exit Sub
- Dim Filename As String = CType(Target.Offset(0, -5).Value2, String)
- Dim DirectoryInfo As New IO.DirectoryInfo(Directory_string)
- If DirectoryInfo.Exists Then
- Dim Explorer As String = $"{Environment.GetEnvironmentVariable("SystemRoot")}\Explorer.exe"
- Process.Start(Explorer, $"/n /e,/select,{DirectoryInfo.FullName}\{Filename}")
- End If
- Cancel = True
- End Sub
- Public Sub WorkbookBeforeCloseEventHandler(
- <InAttribute(), MarshalAs(UnmanagedType.Interface)> Wb As Workbook,
- ByRef Cancel As Boolean)
- Dim hWnd As New HWND_IWin32(New IntPtr(Excel_Application.ActiveWindow.Hwnd))
- If DialogResult.Cancel = MessageBox.Show(hWnd,
- "Close workbook in Excel?", "Find files", MessageBoxButtons.OKCancel) Then
- Cancel = True
- Else
- Delete_Excel_Application = True
- Close_Excel()
- End If
- End Sub
- Sub Close_Excel()
- RemoveHandler Excel_Application.SheetBeforeDoubleClick, AddressOf SheetBeforeDoubleClickEventHandler
- RemoveHandler Excel_Application.WorkbookBeforeClose, AddressOf WorkbookBeforeCloseEventHandler
- Excel_Application.Quit()
- End Sub
- Protected Overrides Sub finalize()
- If Not Delete_Excel_Application Then Close_Excel()
- End Sub
- Sub InitializeWorksheet(title_Worksheet As String)
- If NumberOf_Added_Worksheets = 0 Then
- Worksheet = CType(Workbook.Worksheets(1), Excel.Worksheet)
- Else
- Worksheet = CType(Workbook.Worksheets.Add(), Excel.Worksheet)
- End If
- NumberOf_Added_Worksheets += 1
- Worksheet.Name = title_Worksheet
- cell = Worksheet.Range("A2")
- cell.Font.Size = 16
- End Sub
- Sub Common_ShowWOrkSheet()
- Excel_Application.ActiveWindow.WindowState = Excel.XlWindowState.xlMaximized
- Excel_Application.Visible = True
- Excel_Application.ActiveWindow.Visible = True
- Dim hWnd_Excel As IntPtr = New IntPtr(Excel_Application.ActiveWindow.Hwnd)
- If GetForegroundWindow() <> hWnd_Excel Then
- AppActivate(CInt(GetProcessId_from_hWnd(hWnd_Excel)))
- End If
- End Sub
- Sub Show_FoundFilename(Title_SelectedFloder As String)
- Dim FoundFilenames As List(Of FolderItem) = Me.Find_files.FoundFilenames
- InitializeWorksheet("Find filename")
- If FoundFilenames.Count = 0 Then
- cell.Value = $" { FileNameToBeFound} not found in { Title_SelectedFloder}"
- Excel_Application.ActiveWindow.DisplayGridlines = False
- Exit Sub
- End If
- cell.Value =
- $" Find {FoundFilenames.Count} { FileNameToBeFound} in { Title_SelectedFloder}"
- cell = cell.Offset(1, 0)
- FoundFilenames.Sort(AddressOf Me.Comparer_Folder_Item)
- Call ColumnName()
- For Each Folder_Item As FolderItem In FoundFilenames
- Call ColumnValue(Folder_Item)
- Next
- End Sub
- Sub Show_ModifyFolderItem(Title_SelectedFolder As String)
- Dim ModifiedFolderItems As Collections.Generic.List(Of FolderItem) =
- Me.Find_files.ModifiedFolderItems
- InitializeWorksheet("Modified files")
- Dim Str As String
- If ModifiedFolderItems.Count = 0 Then
- Str = "there is no file"
- Excel_Application.ActiveWindow.DisplayGridlines = False
- ElseIf ModifiedFolderItems.Count = 1 Then
- Str = "there is 1 file"
- Else
- Str = $"there are {ModifiedFolderItems.Count} files"
- End If
- cell.Value = $" From { Time_UpLimitation:yyyy/MM/dd HH:mm:ss}" &
- $" to { Time_DownLimitation:yyyy/MM/dd HH:mm:ss}, " &
- $"{Str} being modified in { Title_SelectedFolder}"
- If ModifiedFolderItems.Count = 0 Then Exit Sub
- cell = cell.Offset(1, 0)
- ModifiedFolderItems.Sort(AddressOf Me.Comparer_Folder_Item)
- Call ColumnName()
- For Each Folder_Item As FolderItem In ModifiedFolderItems
- Call ColumnValue(Folder_Item)
- Next
- End Sub
- Sub Show_Like_Filename_BeFound(Title_SelectedFolder As String)
- Dim Like_Filenames_BeFound As Collections.Generic.List(Of FolderItem) =
- Me.Find_files.Like_Filenames_BeFound
- InitializeWorksheet("Like filename")
- If Like_Filenames_BeFound.Count = 0 Then
- cell.Value = $" No file like { Like_FileNameToBeFound} has been found" &
- $" in { Title_SelectedFolder}"
- Excel_Application.ActiveWindow.DisplayGridlines = False
- Exit Sub
- End If
- cell.Value = $" Find {Like_Filenames_BeFound.Count} files with name like { Like_FileNameToBeFound}" &
- $" in { Title_SelectedFolder}"
- cell = cell.Offset(1, 0)
- Like_Filenames_BeFound.Sort(AddressOf Me.Comparer_Folder_Item)
- Call ColumnName()
- For Each Folder_Item As FolderItem In Like_Filenames_BeFound
- Call ColumnValue(Folder_Item)
- Next
- End Sub
- Const CountOfColumnsToBeAccess As Int32 = 10
- Private Sub Set_FontColor_Depending_on_Dir(Folder_Item As FolderItem)
- Static previous_dir_name As String
- Static ColorSwitch As Boolean
- Dim Dir_Name As String = My.Computer.FileSystem.GetParentPath(Folder_Item.Path)
- If String.Compare(Dir_Name, previous_dir_name) <> 0 Then
- ColorSwitch = Not ColorSwitch
- previous_dir_name = Dir_Name
- End If
- Worksheet.Range(cell, cell.Offset(0, CountOfColumnsToBeAccess)).Font.ColorIndex =
- CInt(If(ColorSwitch, 10, 5))
- End Sub
- Const Const_Directery As String = "___"
- Protected Function Comparer_Folder_Item(x As FolderItem, y As FolderItem) As Integer
- Dim X1 As String
- Dim Y1 As String
- If String.IsNullOrEmpty(x.Path) Then
- X1 = Const_Directery
- Else
- X1 = My.Computer.FileSystem.GetParentPath(x.Path)
- End If
- If String.IsNullOrEmpty(y.Path) Then
- Y1 = Const_Directery
- Else
- Y1 = My.Computer.FileSystem.GetParentPath(y.Path)
- End If
- If X1 > Y1 Then Return 1
- If X1 < Y1 Then Return -1
- Return String.Compare(x.Name, y.Name, ignoreCase:=True)
- End Function
- Sub ColumnName()
- Dim R As Range = cell
- With Worksheet.Range(R, R.Offset(0, CountOfColumnsToBeAccess))
- .HorizontalAlignment = Excel.Constants.xlCenter
- .Font.ColorIndex = 3
- End With
- R.ColumnWidth = 4 * CDbl(R.ColumnWidth)
- R.Value = "File name" : R = R.Next
- R.Value = "LastWriteTime" : R = R.Next
- R.Value = "LastAccessTime" : R = R.Next
- R.Value = "CreationTime" : R = R.Next
- R.Value = "Length" : R = R.Next
- R.ColumnWidth = 21 * CDbl(R.ColumnWidth)
- R.HorizontalAlignment = Excel.Constants.xlLeft
- R.Value = "Directory" : R = R.Next
- cell = cell.Offset(RowOffset:=1, ColumnOffset:=0)
- End Sub
- Sub ColumnValue(Folder_Item As FolderItem)
- Dim FileInfo As IO.FileInfo
- Try
- FileInfo = My.Computer.FileSystem.GetFileInfo(Folder_Item.Path)
- Set_FontColor_Depending_on_Dir(Folder_Item)
- Dim R As Range = cell
- With R
- .Value = FileInfo.Name ' 0
- End With : R = R.Next
- With R
- .Value = FileInfo.LastWriteTime ' 1
- .NumberFormatLocal = "yyyy/MM/dd HH:mm:ss"
- End With : R = R.Next
- With R
- .Value = FileInfo.LastAccessTime ' 2
- .NumberFormatLocal = "yyyy/MM/dd HH:mm:ss"
- End With : R = R.Next
- With R
- .Value = FileInfo.CreationTime ' 3
- .NumberFormatLocal = "yyyy/MM/dd HH:mm:ss"
- End With : R = R.Next
- With R
- .Value = FileInfo.Length ' 4
- End With : R = R.Next
- With R
- .Value = $"{FileInfo.DirectoryName}" ' 5
- End With : R = R.Next
- Catch e As Exception
- Finally
- cell = cell.Offset(1, 0)
- End Try
- End Sub
- End Class
- <setting name="Button2Text" serializeAs="String">
- <value>Selected folder</value>
- </setting>
- <setting name="CheckBox1Text" serializeAs="String">
- <value>FileName</value>
- </setting>
- <setting name="CheckBox2Text" serializeAs="String">
- <value>Modified time</value>
- </setting>
- <setting name="CheckBox3Text" serializeAs="String">
- <value>Like-fileName</value>
- </setting>
- <setting name="DateTimeFormat" serializeAs="String">
- <value>yyyy/MM/dd HH:mm:ss</value>
- </setting>
- <setting name="Form1Text" serializeAs="String">
- <value>Find files in a selected folder</value>
- </setting>
- <setting name="Form1Position" serializeAs="String">
- <value>100,100,1500,1000</value>
- </setting>
- <setting name="SelectedPath" serializeAs="String">
- <value>C:\Windows\System32</value>
- </setting>
- <setting name="Like_FilenameToBeFound" serializeAs="String">
- <value>*.txt</value>
- </setting>
- <setting name="FilenameToBeFound" serializeAs="String">
- <value>Notepad.exe</value>
- </setting>
- <setting name="Bool_Find_Filename" serializeAs="String">
- <value>true</value>
- </setting>
- <setting name="Bool_Like_Filename" serializeAs="String">
- <value>true</value>
- </setting>
- <setting name="Bool_Modified_Files" serializeAs="String">
- <value>true</value>
- </setting>
- <System.Windows.Forms.ApplicationConfigurationSection>
- <add key="DpiAwareness" value="PerMonitorV2" />
- <add key="EnableWindowsFormsHighDpiAutoResizing" value="true" />
- </System.Windows.Forms.ApplicationConfigurationSection>


















留言
張貼留言