Namespace Unique
Public Class FileFunctions
Public Shared Function Exists(ByVal nome As String) As Boolean
Exists = False
Try
Dim w As New System.IO.StreamReader(nome)
Return True
Catch erd As System.IO.FileNotFoundException
Return False
End Try
End Function
Public Shared Sub Rename(ByVal nome1 As String, ByVal nome2 As String)
FileCopy(nome1, nome2)
Kill(nome1)
End Sub
Public Shared Function GetPath(ByVal name As String) As String
Dim s As String = ""
Dim i, a As Int16
For i = 0 To name.Length - 1
If name.Chars(i) = "\" Then
a = i
End If
Next
For i = 0 To a
s = s & name.Chars(i)
Next
GetPath = s
End Function
Public Shared Function GetName(ByVal name As String) As String
Dim s As String = ""
Dim i, a As Int16
For i = 0 To name.Length - 1
If name.Chars(i) = "\" Then
a = i
End If
Next
For i = a To name.Length - 1
If name.Chars(i) <> "." And i < name.Length - 4 Then
s = s & name.Chars(i)
Else
Exit For
End If
Next
GetName = s
End Function
Public Shared Function GetExtension(ByVal name As String) As String
Dim s As String = ""
Dim i, a As Int16
For i = 0 To name.Length - 1
If name.Chars(i) = "." Then
a = i
End If
Next
For i = a To name.Length - 1
s = s & name.Chars(i)
Next
GetExtension = s
End Function
Public Shared Function Pos(ByVal toseek As String, ByVal inwhichfind As String) As Int64
'Algoritmo di ricerca lento
Dim a, b As Int64
Pos = -1
For a = 0 To inwhichfind.Length - 1 'Esamina ogni carattere della stringa inwhichfind
If inwhichfind.Chars(a) = toseek.Chars(0) Then 'Se il carattere numero a è uguale al primo carattere di toseek...
For b = 0 To toseek.Length - 1 '...verifica che tutti i caratteri che lo seguono siano uguali a quelli di toseek
If toseek.Chars(b) = inwhichfind.Chars(a + b) Then
If b = toseek.Length - 1 Then
Return a 'E, in questo caso, ritorna l'indice del primo carattere di toseek in inwhichfind
End If
Else
Pos = -1
Exit For 'Altrimenti annulla il ciclo e continua il controllo
End If
Next
Else
Pos = -1
End If
Next
End Function
Public Shared Function QuickSearch(ByVal ToSeek As String, ByVal InWhichFind As String) As Int16
'Algoritmo di ricerca veloce
Dim S As String = ""
For i As Int16 = 0 To ToSeek.Length - 1
S = S & InWhichFind.Chars(i)
Next
For i As Int64 = ToSeek.Length To InWhichFind.Length - 1
If S = ToSeek Then
Return i
End If
S = S.Remove(0, 1)
S = S & InWhichFind.Chars(i)
Next
End Function
Public Shared Function GetDirSize(ByVal Path As System.String) As Int64
Try
Dim dirdata() As System.IO.DirectoryInfo
Dim filedata() As System.IO.FileInfo
Dim file As System.
IO.
FileInfo
Dim dimension As Int64 = 0
Dim dir As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(Path)
' Scansione dei file della directory
filedata = dir.GetFiles("*.*")
For Each file In filedata
dimension
= dimension
+ file.
Length
Next
' Scansione delle sottodirectory
dirdata = dir.GetDirectories("*")
For Each dir In dirdata
dimension = dimension + GetDirSize(dir.FullName)
Next
Return dimension
Catch
Return -1
End Try
End Function
Public Shared Function Cut(ByVal what As String) As String
'Tronca il nome di un file o di una cartella come
'se si lavorasse in ambiente DOS
Cut = ""
'Se è un nome di file...
If QuickSearch(".", what) > -1 Then
If what.Length <= 12 Then
'Se il nome ha 8 caratteri, lascia invariato
Return what
Else
Dim i As Int16
'Copia i primi sei caratteri del nome
For i = 0 To 5
Cut = Cut & what.Chars(i)
Next
'Aggiunge un ~1
Cut = Cut & "~1"
'Copia l'estensione in coda al nome
For i = QuickSearch(".", what) To what.Length - 1
Cut = Cut & what.Chars(i)
Next
End If
'Se è il nome di una cartella...
Else
If what.Length <= 8 Then
Return what
Else
Dim i As Int16
'Copia i primi sei caratteri del nome
For i = 0 To 5
Cut = Cut & what.Chars(i)
Next
'Aggiunge un ~1
Cut = Cut & "~1"
End If
End If
End Function
Public Shared Function Pascal(ByVal par As String) As String
'Modifica il percorso di un file in modo che sia
'composto da nomi corti
Pascal = ""
Dim i As Int16
Dim c As String = ""
'Inizia il ciclo che controlla ogni carattere
For i = 0 To par.Length - 1
'Se il carattere non è un backslash, lo copia
'nella stringa buffer
If par.Chars(i) <> "\" Then
c = c & par.Chars(i)
Else
'Altrimenti aggiunge al percorso modificato
'il nome corto della stringa buffer
Pascal = Pascal & Cut(c) & "\"
'E setta la stringa buffer a Nothing
c = ""
End If
Next
'Aggiunge in coda alla funzione il nome del file
'convertito in nome corto
Pascal = Pascal & Cut(c)
End Function
End Class
Public Class DateFunctions
Public Structure Data
Dim Year, Month As Int16
Dim Day, Hours, Minutes, Seconds As Byte
Dim Milli As Int16
End Structure
Public Shared Function CurrentYear() As Int32
Dim cont As New System.Windows.Forms.DateTimePicker
Return cont.Value.Year
End Function
Public Shared Function CurrentMonth() As Int16
Dim cont As New System.Windows.Forms.DateTimePicker
Return cont.Value.Month
End Function
Public Shared Function Today() As Int16
Dim cont As New System.Windows.Forms.DateTimePicker
Return cont.Value.Day
End Function
Public Shared Function CurrentHour() As Int16
Dim cont As New System.Windows.Forms.DateTimePicker
Return cont.Value.Hour
End Function
Public Shared Function CurrentMinute() As Int16
Dim cont As New System.Windows.Forms.DateTimePicker
Return cont.Value.Minute
End Function
Public Shared Function CurrentSecond() As Int16
Dim cont As New System.Windows.Forms.DateTimePicker
Return cont.Value.Second
End Function
Public Shared Function CurrentMilli() As Int16
Dim cont As New System.Windows.Forms.DateTimePicker
Return cont.Value.Millisecond
End Function
Public Shared Function Now() As Data
Dim M As Data
M.Day = Today()
M.Hours = CurrentHour()
M.Milli = CurrentMilli()
M.Minutes = CurrentMinute()
M.Month = CurrentMonth()
M.Seconds = CurrentSecond()
M.Year = CurrentYear()
Return M
End Function
End Class
Public Class Code
Public Function Crypt(ByVal s As String) As String
Dim i As Int64
Dim cifra As Int16
Crypt = ""
For i = 0 To s.Length - 1
cifra = Val(CType(i, String).Chars(CType(i, String).Length - 1))
If Asc(s.Chars(i)) + cifra < 255 Then
Crypt = Crypt & Chr(Asc(s.Chars(i)) + cifra)
Else
Crypt = Crypt & s.Chars(i)
End If
Next
Return Crypt
End Function
Public Function DeCrypt(ByVal s As String) As String
Dim i As Int64
Dim cifra As Int16
DeCrypt = ""
For i = 0 To s.Length - 1
cifra = Val(CType(i, String).Chars(CType(i, String).Length - 1))
If Asc(s.Chars(i)) - cifra < 0 Then
DeCrypt = DeCrypt & s.Chars(i)
Else
DeCrypt = DeCrypt & Chr(Asc(s.Chars(i)) - cifra)
End If
Next
Return DeCrypt
End Function
End Class
End Namespace