The base classes in the .net framework are vast and it is commonplace to see coders reinventing the wheel when there is alerady a class or method which will do exactly what they are wanting to do. This is simply just a case of the base class library being so huge.
All of the methods of this class are static and we shall now have a look at these one by one.
So we will write a little method to test each of these static methods. We will also write a console app to test these.
The rest of our test methods and method calls can now be added to out console app. The entire listing is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace PathCode
{
class Program
{
static void Main()
{
Console.SetWindowSize(140, 60);
string testPath = @"C:\Users\david.amour\Documents\TestFolder\Test.txt";
ChangeFileExtension(testPath);
CombinePaths(@"c:\windows", @"temp\test.txt");
GetDirectoryName(testPath);
GetFileExtension(testPath);
GetFileName(testPath);
GetFileNameWithoutExtension(testPath);
GetFullPath(testPath);
GetInvalidFileNameChars();
GetInvalidPathChars();
GetPathRoot(testPath);
GetRandomFileName();
GetTempFileName();
GetTempPath();
HasExtension(testPath);
IsPathRooted(testPath);
IsPathRooted(@"Documents\TestFolder\Test.txt");
Console.Read();
}
static void ShowHeading(string heading)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(heading);
Console.ForegroundColor = ConsoleColor.White;
}
static void ChangeFileExtension(string filePath)
{
ShowHeading("ChangeFileExtension");
Console.WriteLine("The original file is found at {0}", filePath);
Console.WriteLine("The changed file path is {0}", Path.ChangeExtension(filePath, "doc"));
Console.WriteLine();
}
static void CombinePaths(string path1, string path2)
{
ShowHeading("CombinePaths");
Console.WriteLine("{0} combined with {1} gives {2}", path1, path2, Path.Combine(path1, path2));
Console.WriteLine();
}
static void GetDirectoryName(string path)
{
ShowHeading("GetDirectoryName");
Console.WriteLine("The path part of {0} is {1}", path, Path.GetDirectoryName(path));
Console.WriteLine();
}
static void GetFileExtension(string path)
{
ShowHeading("GetFileExtension");
Console.WriteLine("The extension of {0} is {1}", path, Path.GetExtension(path));
Console.WriteLine();
}
static void GetFileName(string path)
{
ShowHeading("GetFileName");
Console.WriteLine("The file name of {0} is {1}", path, Path.GetFileName(path));
Console.WriteLine();
}
static void GetFileNameWithoutExtension(string path)
{
ShowHeading("GetFileNameWithoutExtension");
Console.WriteLine("The file name without an extension of {0} is {1}", path, Path.GetFileNameWithoutExtension(path));
Console.WriteLine();
}
static void GetFullPath(string path)
{
ShowHeading("GetFullPath");
Console.WriteLine("The full path of {0} is {1}", path, Path.GetFullPath(path));
Console.WriteLine();
}
static void GetInvalidFileNameChars()
{
ShowHeading("GetInvalidFileNameChars");
Console.WriteLine("Chars not allowed in filenames are as follows");
Console.WriteLine();
foreach (char c in Path.GetInvalidFileNameChars())
{
Console.WriteLine("ASCII {0} Char = {1}", (int)c, c);
}
Console.WriteLine();
}
static void GetInvalidPathChars()
{
ShowHeading("GetInvalidPathChars");
Console.WriteLine("Chars not allowed in path names are as follows");
Console.WriteLine();
foreach (char c in Path.GetInvalidPathChars())
{
Console.WriteLine("ASCII {0} Char = {1}", (int)c, c);
}
Console.WriteLine();
}
static void GetPathRoot(string path)
{
ShowHeading("GetPathRoot");
Console.WriteLine("The path root of {0} is {1}", path, Path.GetPathRoot(path));
Console.WriteLine();
}
static void GetRandomFileName()
{
ShowHeading("GetRandomFileName");
Console.WriteLine("A random filename is {0}", Path.GetRandomFileName());
Console.WriteLine();
}
static void GetTempFileName()
{
ShowHeading("GetTempFileName");
Console.WriteLine("A temp filename and path to the real file is {0}", Path.GetTempFileName());
Console.WriteLine();
}
static void GetTempPath()
{
ShowHeading("GetTempPath");
Console.WriteLine("The path to the temp folder is {0}", Path.GetTempPath());
Console.WriteLine();
}
static void HasExtension(string path)
{
ShowHeading("HasExtension");
Console.WriteLine("The path {0} has a file extension - {1}", path, Path.HasExtension(path));
Console.WriteLine();
}
static void IsPathRooted(string path)
{
ShowHeading("IsPathRooted");
Console.WriteLine("The result of path {0} when pushed through IsPathRooted is {1}", path, Path.IsPathRooted(path));
Console.WriteLine();
}
}
}
So System.IO.Path is a simple enough class but has many useful methods which we can use actually quite often but only if we know about these methods. Now you do so make sure you use them!