• Profile
  • Resume
  • Portfolio
  • Blog
Avatar

Nelson Silva

A human. Being. Coffee lover.

Blog

I Write My Thoughts Here

Path Class in C#

January 14, 2022 C#

In this post, you will learn how to work with the Path class so that you can use that same knowledge and thus evolve.

  • this class (belonging to the System.IO library) provides various static methods to obtain information about a certain file or directory.
GetFileName() Returns the filename and its extension
GetFileNameWithoutExtension() Returns the filename without the extension
GetExtension() Returns only the file extension
GetFullPath() Returns the entire specified path (absolute path)
  • when you use const when creating an attribute, you are declaring a constant field which after this declaration cannot be modified.
using System;
using System.IO;

namespace Base {
  class PathClass {
    private const string File = "File.txt";

    public void Run() {
      Console.WriteLine($"File name with extension: {Path.GetFileName(File)}");
      Console.WriteLine($"File name without extension: {Path.GetFileNameWithoutExtension(File)}");
      Console.WriteLine($"File extension: {Path.GetExtension(File)}");
      Console.WriteLine($"Absolute path to file: {Path.GetFullPath(File)}");
    }
  }
}

/*
  File name with extension: File.txt
  File name without extension: File
  File extension: .txt
  Absolute path to file: ...\File.txt
*/

Don’t forget to watch the video and you can always read this post in Portuguese.

Happy coding!

Previous Next

Categories

  • Announcements
  • Python
  • C#
  • Java
  • Go

Latest Posts

  • for Loop in Go
    May 5, 2022
  • Switch in Go
    May 4, 2022
  • Comparison Operators in Go
    May 3, 2022
+351913416022
contact@nelsonsilvadev.com
Porto, Portugal
Nelson Silva © 2022 All Rights Reserved