Friday, July 20, 2007

Path and Filename Decomposition function in VB.NET and C#

I had already written Path and Filename Decomposition functions in VB.NET and C# before I found the simple way to do it with the Path class:

filename = Path.GetFileName(Path)
extension = Path.GetExtension(Path)
fullPath = Path.GetFullPath(Path)
directoryName = Path.GetDirectoryName(Path)
FilenameWithoutExt = Path.GetFileNameWithoutExtension(Path)



However, I have need to use these function for similar things that the Path class cannot handle, so I am including them in this posting.

Here are 4 functions in VB.NET and C# that do path and filename decomposition:


1) GetFileNameFromPath will return a filename and its extension from a full directory path.

GetFileNameFromPath("c:\temp\test.txt") returns "test.txt"

GetFileNameFromPath("test.txt") returns "test.txt"

2) GetFileNameWithoutExtFromPath will return a filename without its extension from a full directory path.

GetFileNameWithoutExtFromPath("c:\temp\test.txt") returns "test"

GetFileNameWithoutExtFromPath("test.txt") returns "test"

3) GetDirFromPath will get the directory from a full path.

GetDirFromPath("c:\temp\test.txt") returns "C:\temp"

GetDirFromPath("test.txt") returns ""

4) GetExtensionFromPath will return only the file extension from either a filename or a full path.

GetExtensionFromPath("c:\temp\test.txt") returns ".txt"

GetExtensionFromPath("test.txt") returns ".txt"



VB.NET Code:


Public Function GetDirFromPath(ByVal path As String) _
As String

Try
Return path.Substring(0, path.LastIndexOf("\") + 1)

Catch ex As Exception
' error
Return ""
End Try

End Function
'
Public Function GetFileNameFromPath(ByVal path _
As String) As String

Try
Return path.Substring(path.LastIndexOf("\") + 1)
Catch ex As Exception
' error
Return ""
End Try

End Function
'
Public Function GetFileNameWithoutExtFromPath(ByVal path _
As String) As String

Try
Dim filename As String = _
path.Substring(path.LastIndexOf("\") + 1)
Dim pos As Integer = filename.LastIndexOf(".")

If pos <> -1 Then
Return filename.Substring(0, pos)
Else
Return filename
End If

Catch ex As Exception
' error
Return ""
End Try

End Function
'
Public Function GetExtensionFromPath(ByVal path _
As String) As String

Try
Dim pos As Integer = _
path.LastIndexOf(".")
If pos <> -1 Then
Return path.Substring(pos)
Else
Return ""
End If

Catch ex As Exception
' error
Return ""
End Try
End Function



C# Code:


public String GetDirFromPath(String path)
{
try
{
return path.Substring(0, path.LastIndexOf("\\") + 1);
}
catch (Exception ex)
{
// error
return "";
}
}


public String GetFileNameFromPath(String path)
{
try
{
return path.Substring(
path.LastIndexOf("\\") + 1);
}
catch (Exception ex)
{
// error
return "";
}

}

public String GetFileNameWithoutExtFromPath(String path)
{
try
{
String filename = path.Substring(
path.LastIndexOf("\\") + 1);
int pos = filename.LastIndexOf(".");

if (pos != -1)
return filename.Substring(0, pos);
else
return filename;
}

catch (Exception ex)
{
// error
return "";
}

}

public String GetExtensionFromPath(String path)
{
try
{
int pos = path.LastIndexOf(".");
if (pos != -1)
return path.Substring(pos);
else
return "";
}

catch (Exception ex)
{
// error
return "";
}

}

3 comments:

Anonymous said...

Thanks - that was useful!

Anonymous said...

thank you great stuff!

Anonymous said...

Where do you Specify the Path of the Dir you are looking at?