· Un AppActivate distinto, en mi opinión mejor, se usa por el nombre del proceso, con posibilidad de seleccionar el proceso por el título de la ventana
de dicho proceso:
#Region " Activate APP "
' [ Activate APP Function ]
'
' // By Elektro H@cker
'
' Examples :
' ActivateAPP("notepad.exe")
' ActivateAPP("notepad.exe", "Notepad Sub-Window Title")
' MsgBox(ActivateAPP("notepad"))
Private Function ActivateAPP(ByVal ProcessName As String, _
Optional ByVal WindowTitle As String = Nothing) As Boolean
If ProcessName.ToLower.EndsWith(".exe") Then ProcessName = ProcessName.Substring(0, ProcessName.Length - 4)
Dim ProcessTitle As String = Nothing
Dim ProcessArray = Process.GetProcessesByName(ProcessName)
If ProcessArray.Length = 0 Then : Return False ' ProcessName not found
ElseIf ProcessArray.Length > 1 AndAlso Not WindowTitle Is Nothing Then
For Each Title In ProcessArray
If Title.MainWindowTitle.Contains(WindowTitle) Then _
ProcessTitle = Title.MainWindowTitle
Next
Else : ProcessTitle = ProcessArray(0).MainWindowTitle
End If
AppActivate(ProcessTitle)
Return True ' Window activated
End Function
#End Region
· Escribe texto en un Log
#Region " Write Log "
' [ Write Log Function ]
'
' // By Elektro H@cker
'
' Examples :
' WriteLog("Application started", InfoType.Information)
' WriteLog("Application got mad", InfoType.Critical)
Dim LogFile = CurDir() & "\" & System.Reflection.Assembly.GetExecutingAssembly.GetName().Name & ".log"
Public Enum InfoType
Information
Exception
Critical
None
End Enum
Private Function WriteLog(ByVal Message As String, ByVal InfoType As InfoType) As Boolean
Dim LocalDate As String = My.Computer.Clock.LocalTime.ToString.Split(" ").First
Dim LocalTime As String = My.Computer.Clock.LocalTime.ToString.Split(" ").Last
Dim LogDate As String = "[ " & LocalDate & " ] " & " [ " & LocalTime & " ] "
Dim MessageType As String = Nothing
Select Case InfoType
Case InfoType.Information : MessageType = "Information: "
Case InfoType.Exception : MessageType = "Error: "
Case InfoType.Critical : MessageType = "Critical: "
Case InfoType.None : MessageType = ""
End Select
Try
My.Computer.FileSystem.WriteAllText(LogFile, vbNewLine & LogDate & MessageType & Message & vbNewLine, True)
Return True
Catch ex As Exception
'Return False
Throw New Exception(ex.Message)
End Try
End Function
#End Region
· Cierra un proceso (No lo mata)
#Region " Close Process Function "
' [ Close Process Function ]
'
' Examples :
'
' Close_Process(Application.ExecutablePath)
' Close_Process("notepad.exe")
' Close_Process("notepad", False)
Private Function Close_Process(ByRef Process_Name As String, _
Optional ByVal OnlyFirstFound As Boolean = True) As Boolean
If Process_Name.ToLower.EndsWith(".exe") Then Process_Name = Process_Name.Substring(0, Process_Name.Length - 4)
Dim proc() As Process = Process.GetProcessesByName(Process_Name)
If Not OnlyFirstFound Then
For proc_num As Integer = 0 To proc.Length - 1
Try : proc(proc_num).CloseMainWindow() _
: Catch : Return False : End Try ' One of the processes can't be closed
Next
Return True
Else
Try : proc(0).CloseMainWindow() : Return True ' Close message sent to the process
Catch : Return False : End Try ' Can't close the process
End If
Return Nothing ' ProcessName not found
End Function
#End Region
· Buscar coincidencias de texto usando expresiones regulares
#Region " Find RegEx "
' [ Find RegEx Function ]
'
' // By Elektro H@cker
'
' Examples :
' If Find_RegEx("abcdef", "^[A-Z]+$") Then MsgBox("Yes") Else MsgBox("No") ' Result: No
' If Find_RegEx("abcdef", "^[A-Z]+$", True) Then MsgBox("Yes") Else MsgBox("No") ' Result: Yes
Private Function Find_RegEx(ByVal str As String, ByVal Pattern As String, _
Optional ByVal Ignorecase As Boolean = False) As Boolean
Dim RegExCase As System.Text.RegularExpressions.RegexOptions
If Ignorecase Then _
RegExCase = System.Text.RegularExpressions.RegexOptions.IgnoreCase _
Else RegExCase = System.Text.RegularExpressions.RegexOptions.None
Dim RegEx As New System.Text.RegularExpressions.Regex(Pattern, RegExCase)
Return RegEx.IsMatch(str)
End Function
#End Region
· Leer un texto línea por línea (For each line...) con posibilidad de saltar líneas en blanco.
#Region " Read TextFile Libe By Line "
' [ Read TextFile Libe By Line ]
'
' // By Elektro H@cker
'
' Examples :
' Read_TextFile_Libe_By_Line("C:\Test.txt")
' Read_TextFile_Libe_By_Line("C:\Test.txt", True)
Private Sub Read_TextFile_Libe_By_Line(ByVal TextFile As String, _
Optional ByVal Read_Blank_Lines As Boolean = False)
Dim Line As String = Nothing
Dim Text
As IO.
StreamReader = IO.
File.
OpenText(TextFile
) Dim RegEx As New System.Text.RegularExpressions.Regex("^\s+$")
Do Until Text.EndOfStream
Line = Text.ReadLine()
If (Not Read_Blank_Lines _
AndAlso _
(Not Line = "" _
And Not RegEx.IsMatch(Line))) _
OrElse Read_Blank_Lines Then
' Do things here...
MsgBox(Line)
End If
Loop
Text.Close() : Text.Dispose()
End Sub
#End Region