.Net application development specialists
asp.net, c#, vb.net, html, javascript, jquery, html, xhtml, css, oop, design patterns, sql server, mvc and much more
contact: admin@paxium.co.uk

Paxium is the company owned by myself, Dave Amour and used for providing IT contract development services including


  • Application development - Desktop, Web, Services - with Classic ASP, Asp.net WebForms, Asp.net MVC, Asp.net Core
  • Html, Css, JavaScript, jQuery, React, C#, SQL Server, Ado.net, Entity Framework, NHibernate, TDD, WebApi, GIT, IIS
  • Database schema design, implementation & ETL activities
  • Website design and hosting including email hosting
  • Training - typically one to one sessions
  • Reverse Engineering and documentation of undocumented systems
  • Code Reviews
  • Performance Tuning
  • Located in Cannock, Staffordshire
Rugeley Chess Club Buying Butler Cuckooland Katmaid Pet Sitting Services Roland Garros 60 60 Golf cement Technical Conformity Goofy MaggieBears Vacc Track Find Your Smart Phone eBate Taylors Poultry Services Lafarge Rebates System Codemasters Grid Game eBate DOFF

The methods of System.IO.Path

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.

One of these classes which is often underused and overlooked is System.IO.Path

All of the methods of this class are static and we shall now have a look at these one by one.

These methods are as follows:

  • ChangeFileExtension
  • CombinePaths
  • GetDirectoryName
  • GetFileExtension
  • GetFileName
  • GetFileNameWithoutExtension
  • GetFullPath
  • GetInvalidFileNameChars
  • GetInvalidPathChars
  • GetPathRoot
  • GetRandomFileName
  • GetTempFileName
  • GetTempPath
  • HasExtension
  • IsPathRooted

So we will write a little method to test each of these static methods.  We will also write a console app to test these.

So our first method will look like this:

        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();

       }


Within here we have made a call to a method called ShowHeading.  This is  a method we will use which just outputs a heading to the console in yellow which is just for aesthetic reasons and to make the code output more readable.

This ShowHeading method will be as follows:

        static void ShowHeading(string heading)

        {

            Console.ForegroundColor = ConsoleColor.Yellow;

            Console.WriteLine(heading);

            Console.ForegroundColor = ConsoleColor.White;

        }


Now to test this method, and the other methods once we write them, we will need a console app with a Main method as follows:

        static void Main()

        {

            Console.SetWindowSize(140, 60);

 

            string testPath = @"C:\Users\david.amour\Documents\TestFolder\Test.txt";

 

            ChangeFileExtension(testPath);

 

            Console.Read();

        }


Of course you will need to change the testPath to point to some test file on your system.  If you then run this you wil see output something like this:

 

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();
        }
    }
}
 


And the output when we run this code should look like this:

 

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!