Path class in VB.NET

This article will explain about the Path class and its use.
  • 3272

Introduction

This article will explain the Path class and its use. The System.IO namespace is one of the most significant namespaces used for working with file path in the .Net Framework. Let us see about the class. A path is a string that provides the location of a file or directory. A path does not necessarily point to a location on disk. The exact format of a path is determined by the current platform. A path can refer to a file or just a directory. The specified path can also refer to a relative path or a Universal Naming Convention (UNC) path for a server and share name.

Path Class

The Path class is a utility class used to manipulate path names. The Path class provides methods for manipulating a file system path. This class provides handful of static methods that make dealing with path a snap.

Some of the most useful methods of the Path class are:

S.No Methods Description
1 ChangeExtension This method Takes a path and returns a new path with the file name's extension changed.
2 Combine This method is used to Combine two compatible path strings.
3 GetDirectoryName Returns the name of the directory in the specified path.
4 GetExtension Returns the name of the file extension in the specified path.
5 GetFileName Returns the name of the file in the specified path.
6 GetFileNameWithoutExtension This method is used to Return the file name without the extension in the specified path.
7 GetFullPath Returns a full path to the specified path. This method can be used to resolve relative paths.
8 GetPathRoot Returns the root directory name in the specified path.
9 HasExtension Indicates whether a specified path's file name has an extension.
10 IsPathRooted Indicates whether the specified path includes a root directory.

ChangeExtension Method

This method Takes a path and returns a new path with the file name's extension changed.

 

Dim FileName__1 As String = "C:\mydir\myfile.com.extension"

       Dim res As String

        res =Path.ChangeExtension(FileName__1,".old")

       Console.WriteLine("ChangeExtension: {0}", res)

Combine Method

This method is used to Combine two compatible path strings.

Dim paths As String() = {"d:\archives","2011", "media", "images"}

       Dim fullPath As String = Path.Combine(paths)

       Console.WriteLine(fullPath)

GetDirectoryName

This method is used to return the name of the directory in the specified path.

Dim filePath As String = "C:\MyDir\MySubDir\myfile1.ext"

       Dim directoryNameAs String

       While filePath IsNot Nothing

            directoryName = Path.GetDirectoryName(filePath)

           Console.WriteLine("GetDirectoryName: {0}", directoryName)

       End While

GetExtension

Returns the name of the file extension in the specified path.

Dim fileName2 As String = "C:\mydir\myfile1.txt"

       Dim extension As String

        extension =Path.GetExtension(fileName)

       Console.WriteLine("GetExtension: {0}", extension)

GetFileName

This method is used to return the name of the file in the specified path.

Dim fileName As String = "C:\mydir\myfile1.txt"

       Dim result As String

        result =Path.GetFileName(fileName)

       Console.WriteLine("GetFileName: {0} ", result)

GetFileNameWithoutExtension

This method is used to return the file name without the extension in the specified path.

Dim fileName As String = "C:\mydir\myfile1.txt"

       Dim result As String

        result =Path.GetFileNameWithoutExtension(fileName)

       Console.WriteLine("GetFileName: {0} ", result)

 

GetFullPath

This method is used to Return a full path to the specified path. This method can be used to resolve relative paths.

Dim path1 As String = "mydir"

       Dim fullPath As String

        fullPath =Path.GetFullPath(path1)

       Console.WriteLine("GetFullPath: {0}", fullPath)

GetPathRoot

This method is used to return the root directory name in the specified path.

Dim fileName As String = "C:\mydir\myfile1.txt"

       Dim result As String

        result =Path.GetPathRoot(fileName)

       Console.WriteLine("GetFileName: {0} ", result)

HasExtension

This method indicates whether a specified path's file name has an extension.

Dim fileName As String = "C:\mydir\myfile1.txt"

       Dim result As Boolean

        result =Path.HasExtension(fileName)

       Console.WriteLine("GetFileName: {0} ", result)

IsPathRooted

This method indicates whether the specified path includes a root directory.

Dim fileName As String = "mydir\myfile.txt"

       Dim result As Boolean

        result =Path.IsPathRooted(fileName)

       Console.WriteLine("GetFileName: {0} ", result)

Example

Imports System.IO

Module Module1

 

    Sub Main()

        ' ChangeExtension Method

 

        Dim FileName__1 As String = "C:\mydir\myfile.com.extension"

        Dim res As String

        res = Path.ChangeExtension(FileName__1, ".old")

        Console.WriteLine("ChangeExtension: {0}", res)

 

        ' Combined Method

 

 

        Dim paths As String() = {"d:\archives", "2011", "media", "images"}

        Dim fullPath As String = Path.Combine(paths)

        Console.WriteLine(fullPath)

 

        'GetDirectoryName

 

        Dim filePath As String = "C:\MyDir\MySubDir\myfile1.ext"

        Dim directoryName As String

        While filePath IsNot Nothing

            directoryName = Path.GetDirectoryName(filePath)

            Console.WriteLine("GetDirectoryName: {0}", directoryName)

        End While

 

        'GetExtension

 

        Dim fileName2 As String = "C:\mydir\myfile1.txt"

        Dim extension As String

        extension = Path.GetExtension(fileName)

        Console.WriteLine("GetExtension: {0}", extension)

 

        'GetFile Name

 

        Dim fileName As String = "C:\mydir\myfile1.txt"

        Dim result As String

        result = Path.GetFileName(fileName)

        Console.WriteLine("GetFileName: {0} ", result)

 

 

        'GetFileNameWithoutExtension

 

        Dim fileName As String = "C:\mydir\myfile1.txt"

        Dim result As String

        result = Path.GetFileNameWithoutExtension(fileName)

        Console.WriteLine("GetFileName: {0} ", result)

 

        'GetFullPath

 

        Dim path1 As String = "mydir"

        Dim fullPath As String

        fullPath = Path.GetFullPath(path1)

        Console.WriteLine("GetFullPath: {0}", fullPath)

 

        'GetPathRoot

 

        Dim fileName As String = "C:\mydir\myfile1.txt"

        Dim result As String

        result = Path.GetPathRoot(fileName)

        Console.WriteLine("GetFileName: {0} ", result)

 

        'HasExtension

 

        Dim fileName As String = "C:\mydir\myfile1.txt"

        Dim result As Boolean

        result = Path.HasExtension(fileName)

        Console.WriteLine("GetFileName: {0} ", result)

 

        'IsPathRooted

 

        Dim fileName As String = "mydir\myfile.txt"

        Dim result As Boolean

        result = Path.IsPathRooted(fileName)

        Console.WriteLine("GetFileName: {0} ", result)

    End Sub

End Module

 

Conclusion:

The Path class is very useful for working with files and directories. If there is any mistake in this article then please notify me. I expect your valuable comments and feedback about this article.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.