Working with Directories in C#


.Net Programming with Al Jensen

The System.IO.Directory class gives us the ability to manipulate the current working directory. There are two static methods for manipulating the current working directory, GetCurrentDirectory() and SetCurrentDirectory().

static void Main(string[] args)
        {
            string dirName;

            //assign current directory to a string 
            dirName = System.IO.Directory.GetCurrentDirectory();

             //output to screenConsole.WriteLine("Current directory is {0}", dirName);

            //get the root using Path methodConsole.WriteLine("Path root is {0}", System.IO.Path.GetPathRoot(dirName));

            //change the current working directory
            System.IO.Directory.SetCurrentDirectory(@"C:");

            Console.WriteLine(System.IO.Directory.GetCurrentDirectory());

        }

We can also access the current application directory using the CurrentDirectory property in the System.Environment class.

                static void Main(string[] args)
        {
            string dirPath;

            Console.WriteLine("Application directory {0}", System.Environment.CurrentDirectory);

            System.IO.Directory.SetCurrentDirectory(@"C:");

            Console.WriteLine("Application directory {0}", System.Environment.CurrentDirectory);

            //get the fully qualified path of the system directory
            dirPath = System.Environment.SystemDirectory;

            //change working direcory to system…

View original post 462 more words

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s