|
By: Paul S Cilwa |
Posted: 4/10/2026 |
|
Page Views: 25 |
| Hashtags: #Namtira #VisualBasic #VBNET #ClassLibrary #NamtiraLib #SuperFiles #FileSystem #FileInfo #DirectoryInfo |
| Extension methods for files and folders: rename, relative paths, attribute checks, and more. |
| Estimated reading time: 3 minute(s) (598 words) |
| Module SuperFiles |
| Member | Parameters | Example |
| BaseName | (extends FileInfo or DirectoryInfo) | myFile.BaseName → "Sunset" |
| RenameTo | aNewName As String (extends FileInfo) | myFile.RenameTo("Beach.jpg") |
| IsSubfolderOf | PossibleChildFolder (extends DirectoryInfo or String) | parent.IsSubfolderOf(child) |
| RelativePath | RelativeToPath As String (extends String, FileInfo, or DirectoryInfo) | myFile.RelativePath("C:\Photos\") |
| IsHidden | (extends DirectoryInfo or FileInfo) | myFile.IsHidden → True |
| IsSystem | (extends DirectoryInfo or FileInfo) | myFolder.IsSystem → False |
Imports System.IO
Imports System.Runtime.CompilerServices
Imports Microsoft.VisualBasic.FileIO
Module SuperFiles
End Module
BaseName
Returns the file or folder name without its extension. Two overloads
cover both FileInfo and DirectoryInfo objects.
<Extension()>
Public Function BaseName(F As FileInfo) As String
Return F.Name.Left(F.Name.Length - F.Extension.Length)
End Function
<Extension()>
Public Function BaseName(D As DirectoryInfo) As String
Return D.Name.Left(D.Name.Length - D.Extension.Length)
End Function
Note how these make use of the Left extension from
SuperStrings.
RenameTo
Renames a file and updates the FileInfo reference to point to the
new name, so you can keep using it without creating a new object. The
parameter is passed ByRef so the caller's variable is updated
in place.
<Extension()>
Public Sub RenameTo(ByRef aFileInfo As FileInfo, aNewName As String)
FileSystem.RenameFile(aFileInfo.FullName, aNewName)
aFileInfo = New FileInfo(Path.Combine(aFileInfo.DirectoryName, aNewName))
End Sub
IsSubfolderOf
Checks whether one folder is a child (or grandchild, etc.) of another.
The DirectoryInfo overload normalizes paths and uses URI comparison
for reliable results. A convenience overload accepts plain path strings.
<Extension()>
Public Function IsSubfolderOf(ParentFolderPath As String,
PossibleChildFolderPath As String) As Boolean
If ParentFolderPath > "" And PossibleChildFolderPath > "" Then
Return IsSubfolderOf(New DirectoryInfo(ParentFolderPath),
New DirectoryInfo(PossibleChildFolderPath))
Else
Return False
End If
End Function
<Extension()>
Public Function IsSubfolderOf(ParentFolder As DirectoryInfo,
PossibleChildFolder As DirectoryInfo) As Boolean
Dim parentPath As String = ParentFolder.FullName _
.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) _
.ToLowerInvariant()
Dim childPath As String = PossibleChildFolder.FullName _
.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) _
.ToLowerInvariant()
If String.Equals(parentPath, childPath,
StringComparison.OrdinalIgnoreCase) Then
Return True
End If
Dim parentUri As New Uri(ParentFolder.FullName + Path.DirectorySeparatorChar)
Dim childUri As New Uri(PossibleChildFolder.FullName + Path.DirectorySeparatorChar)
Return parentUri.IsBaseOf(childUri) AndAlso
Not parentUri.AbsoluteUri.Equals(childUri.AbsoluteUri,
StringComparison.OrdinalIgnoreCase)
End Function
RelativePath
Calculates the relative path from one location to another. The base
overload works with plain strings; two additional overloads accept
FileInfo and DirectoryInfo objects for convenience.
<Extension>
Public Function RelativePath(ByVal SourcePath As String,
ByVal RelativeToPath As String) As String
Dim Source As New Uri(SourcePath)
Dim Relative As New Uri(RelativeToPath)
Dim Result As String = Source.MakeRelativeUri(Relative).ToString()
Result = Uri.UnescapeDataString(Result.Replace("/", Path.DirectorySeparatorChar))
Return Result
End Function
<Extension>
Public Function RelativePath(ByVal Source As FileInfo,
ByVal RelativeToPath As String) As String
Return RelativePath(Source.FullName, RelativeToPath)
End Function
<Extension>
Public Function RelativePath(ByVal TestPath As DirectoryInfo,
ByVal RelativeToPath As String) As String
Return RelativePath(TestPath.FullName, RelativeToPath)
End Function
Attribute Checks
IsHidden and IsSystem check whether the corresponding
file-system attribute is set. Each has overloads for both DirectoryInfo
and FileInfo.
<Extension()>
Public Function IsHidden(D As DirectoryInfo) As Boolean
Return (D.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden
End Function
<Extension()>
Public Function IsHidden(F As FileInfo) As Boolean
Return (F.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden
End Function
<Extension()>
Public Function IsSystem(D As DirectoryInfo) As Boolean
Return (D.Attributes And FileAttributes.System) = FileAttributes.System
End Function
<Extension()>
Public Function IsSystem(F As FileInfo) As Boolean
Return (F.Attributes And FileAttributes.System) = FileAttributes.System
End Function