|
- Gestire un path (percorso di un file) - |
|||
| COSA SERVE PER QUESTO TUTORIAL | |||
| Download | Chiedi sul FORUM | Glossario | Basica conoscenza di VB .NET | ||
| Classe Path | |||
UNA CLASSE PER GESTIRE UN PERCORSO Utile classe per ottenere da un percorso nome, estensione, titolo, cartelle del file e altro. Codice:
''' <summary>Semplice classe che permette di ricavare informazioni da una path
''' di un file</summary>
Public Class Path
''' <summary>La stringa della path</summary>
Protected strPath As String
''' <summary>Costruttore</summary>
Public Sub New(ByVal Path As String)
Me.Path = Path
End Sub
''' <summary>Restituisce e imposta il path, verifica inoltre la correttezza
''' della stringa</summary>
Public Property Path() As String
Get
Return strPath
End Get
Set(ByVal value As String)
strPath = value.Trim
If strPath.Substring(strPath.Length - 1, 1) = "\" Then
Throw New Exception("Only file-path")
ElseIf strPath Is Nothing OrElse strPath = [String].Empty Then
Throw New Exception("Void path received")
ElseIf IsUNC And InStrT(strPath, "\") < 4 Then
Throw New Exception("Only file-path")
End If
End Set
End Property
''' <summary>Restituisce True se è un percorso UNC
''' (es. "\\mio_pc\cartella\file.ext")</summary>
Public ReadOnly Property IsUNC() As Boolean
Get
Return strPath.Substring(0, 2) = "\\"
End Get
End Property
''' <summary>Restituisce True se il file specificato esiste</summary>
Public ReadOnly Property Exists() As Boolean
Get
Return System.IO.File.Exists(strPath)
End Get
End Property
''' <summary>Restituisce la cartella in cui il file è contenuto
''' (es. "c:\miacartella\")</summary>
Public ReadOnly Property Directory() As String
Get
Return strPath.Substring(0, strPath.LastIndexOf("\") + 1)
End Get
End Property
''' <summary>Restituisce il nome del file (es. "file.ext")</summary>
Public ReadOnly Property FileName() As String
Get
Return strPath.Substring(strPath.LastIndexOf("\") + 1)
End Get
End Property
''' <summary>Restituisce il titolo del file (es. "file")</summary>
Public ReadOnly Property Title() As String
Get
Title = Me.FileName
If Not Title.IndexOf(".") = -1 Then _
Title = Title.Substring(0, Title.LastIndexOf("."))
End Get
End Property
''' <summary>Restituisce l'estensione del file (es. "ext")</summary>
Public ReadOnly Property Extension() As String
Get
If Title.IndexOf(".") = -1 Then Return [String].Empty
Return strPath.Substring(strPath.LastIndexOf(".") + 1)
End Get
End Property
''' <summary>Restituisce l'ennesima directory superiore
''' (es. New Path("c:\a\b\c\d\e\f.ext").Parent(2) = "c:\a\b\c\")</summary>
''' <param name="Index">L'indice della cartella superiore</param>
Public ReadOnly Property Parent(Optional ByVal Index As Integer = 1) As String
Get
If Index < 0 Then Throw New Exception("Index can't be negative")
If Index >= InStrT(strPath.Replace("\\", ""), "\") Then _
Throw New Exception("The parent you requested is over the root directory")
Parent = Me.Directory
Dim C1 As Integer, strTPath As String
strTPath = strPath
For C1 = 1 To Index
strPath = Parent.Substring(0, Parent.Length - 1)
Parent = Me.Directory
Next C1
strPath = strTPath
End Get
End Property
''' <summary>Restituisce il numero totale di volte che strSearch
''' appare in str</summary>
''' <param name="str">Stringa in cui effettuare la ricerca</param>
''' <param name="strSearch">Stringa da cercare</param>
Private Function InStrT(ByVal str As String, ByVal strSearch As String) As Integer
Return (str.Length - str.Replace(strSearch, "").Length) / strSearch.Length
End Function
End Class
|
|||
| << INDIETRO | by VeNoM00 | ||