How to Set Current directory and how to get root directory

The following code example determines How to set current directory and how to get root directory with using static class directory.

Step 1:-Add the following namespaces

using System;
using System.IO;

Step 2:-Initilizes variables

Create string for a directory. This value should be an existing directory
// or the sample will throw a DirectoryNotFoundException.
string dir = @"C:\test";

Step 3:-Check the existence , Deletion and Creation of directories

//Set the current directory

Step 3:-TO display result

Console.WriteLine("Root directory: {0}", Directory.GetDirectoryRoot(dir));
Console.WriteLine("Current directory: {0}", Directory.GetCurrentDirectory());

Special Note:- Directory class is static and all methods can be called without creating any instance of this class


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: