Export (0) Print
Expand All
1 out of 10 rated this helpful - Rate this topic

Directory.GetCurrentDirectory Method

Gets the current working directory of the application.

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

public static string GetCurrentDirectory ()
public static String GetCurrentDirectory ()
public static function GetCurrentDirectory () : String
Not applicable.

Return Value

A string containing the path of the current working directory.
Exception typeCondition

UnauthorizedAccessException

The caller does not have the required permission.

NotSupportedException

The operating system is Windows CE, which does not have current directory functionality.

This method is available in the .NET Compact Framework, but is not currently supported.

The current directory is distinct from the original directory, which is the one from which the process was started.

For a list of common I/O tasks, see Common I/O Tasks.

Windows Mobile 2003 for Pocket PC, Windows Mobile 2003 for Smartphone, Windows CE Platform Note: Because some mobile device operating systems do not have current directory functionality, this method is not supported.

The following code example demonstrates the GetCurrentDirectory method.

using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        try 
        {
            // Get the current directory.
            string path = Directory.GetCurrentDirectory();
            string target = @"c:\temp";
            Console.WriteLine("The current directory is {0}", path);
            if (!Directory.Exists(target)) 
            {
                Directory.CreateDirectory(target);
            }

            // Change the current directory.
            Environment.CurrentDirectory = (target);
            if (path.Equals(Directory.GetCurrentDirectory())) 
            {
                Console.WriteLine("You are in the temp directory.");
            } 
            else 
            {
                Console.WriteLine("You are not in the temp directory.");
            }
        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}

import System.*;
import System.IO.*;

class Test
{
    public static void main(String[] args)
    {
        try {
            // Get the current directory.
            String path = Directory.GetCurrentDirectory();
            String target = "c:\\temp";

            Console.WriteLine("The current directory is {0}", path);
            if (!(Directory.Exists(target))) {
                Directory.CreateDirectory(target);
            }

            // Change the current directory.
            Environment.set_CurrentDirectory(target);
            if (path.Equals(Directory.GetCurrentDirectory())) {
                Console.WriteLine("You are in the temp directory.");
            }
            else {
                Console.WriteLine("You are not in the temp directory.");
            }
        }
        catch (System.Exception e) {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    } //main
} //Test

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.