-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionPack.vb
125 lines (125 loc) · 5.13 KB
/
FunctionPack.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
Imports System.IO
Public Class FunctionPack
#Region " #: Date functions :# "
Private Function SecondsToDate(ByVal Seconds As Double, Optional ByVal showSeconds As Boolean = False) As String
If Seconds < 60 Then
Return Seconds & " seconds"
Else
If showSeconds Then
Return Date.FromOADate(Seconds / 86400).ToLongTimeString
Else
Return Date.FromOADate(Seconds / 86400).ToShortTimeString
End If
End If
End Function
Private Function MillisecondsToDate(ByVal Milliseconds As Double, Optional ByVal showSeconds As Boolean = False) As String
Dim Seconds As Double = Math.Round(Milliseconds / 1000)
If Seconds < 60 Then
Return Seconds & " seconds"
Else
If showSeconds Then
Return Date.FromOADate(Seconds / 86400).ToLongTimeString
Else
Return Date.FromOADate(Seconds / 86400).ToShortTimeString
End If
End If
End Function
#End Region
#Region " #: FileIO functions :# "
Function GetFolderSize(ByVal DirPath As String, _
Optional ByVal IncludeSubFolders As Boolean = True) As Long
Dim lngDirSize As Long
Dim objFileInfo As FileInfo
Dim objDir As DirectoryInfo = New DirectoryInfo(DirPath)
Dim objSubFolder As DirectoryInfo
Try
For Each objFileInfo In objDir.GetFiles()
lngDirSize += objFileInfo.Length
Next
If IncludeSubFolders Then
For Each objSubFolder In objDir.GetDirectories()
lngDirSize += GetFolderSize(objSubFolder.FullName)
Next
End If
Catch Ex As Exception
End Try
Return lngDirSize
End Function
#End Region
#Region " #: String functions :# "
Public Shared Function ShrinkText(ByVal input As String, ByVal value As Integer) As String
If input.Length >= value Then
Dim temp As String = input
Do Until temp.Length = value - 3
temp = temp.Remove(temp.Length - 1, 1)
Loop
Return temp & "..."
Else
Return input
End If
End Function
#End Region
#Region " #: Other stuff :# "
Public Class ByteConverter
Dim binaryTypesArray As String() = {"KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
Enum BinaryTypes
KB = 0
MB = 1
GB = 2
TB = 3
PB = 4
EB = 5
ZB = 6
YB = 7
End Enum
Public Function ConvertByte(ByVal bytes As Double) As String
Dim mString As String = ""
For i As Integer = 0 To binaryTypesArray.Length - 1
Dim index As Integer = (binaryTypesArray.Length - 1) - i
If bytes <= 1023 Then
mString = bytes & " bytes"
Exit For
ElseIf bytes >= 1024 ^ (index + 1) Then
mString = Math.Round(bytes / (1024 ^ (index + 1)), 2) & " " & binaryTypesArray(index)
Exit For
End If
Application.DoEvents()
Next
Return mString
End Function
Public Function ConvertByte(ByVal bytes As Double, ByVal outputBinaryType As BinaryTypes)
Dim index As Integer = outputBinaryType
Return Math.Round(bytes / (1024 ^ (index + 1)), 2) & " " & binaryTypesArray(index)
End Function
End Class
#End Region
#Region " #: Graphics functions :# "
Public Class GraphicsHandler
Public Shared Function MakeGradient(ByVal StartColor As Color, ByVal EndColor As Color, ByVal width As Integer, ByVal height As Integer, Optional ByVal LinearGradientMode As Drawing2D.LinearGradientMode = Drawing2D.LinearGradientMode.Horizontal) As Image
Dim sizeOfControl As New Size(width, height)
Dim resultImage As New Bitmap(width, height, Imaging.PixelFormat.Format32bppArgb)
Dim g As Graphics = Graphics.FromImage(resultImage)
g.SmoothingMode = Drawing2D.SmoothingMode.HighSpeed
g.InterpolationMode = Drawing2D.InterpolationMode.Low
g.FillRectangle(New Drawing2D.LinearGradientBrush(New Rectangle(New Point(0, 0), sizeOfControl), StartColor, EndColor, LinearGradientMode), New Rectangle(New Point(0, 0), sizeOfControl))
Return resultImage
g.Dispose() : g.Flush() : g = Nothing
sizeOfControl = Nothing
resultImage.Dispose() : resultImage = Nothing
End Function
Public Function IconFromFilePath(ByVal filePath As String) As Bitmap
Dim result As Icon = Nothing
If Not IsNothing(filePath) Or Not filePath = "" Then
Try
result = Icon.ExtractAssociatedIcon(filePath)
Catch ''# swallow and return nothing. You could supply a default Icon here as well
result = Nothing
End Try
Return result.ToBitmap
Else
Return Nothing
End If
End Function
End Class
#End Region
End Class