Export (0) Print
Expand All

Directory.GetCurrentDirectory Method

Gets the current working directory of the application.

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

public static string GetCurrentDirectory()

Return Value

Type: System.String
A string that contains the path of the current working directory, and does not end with a backslash ("\").

ExceptionCondition
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.

Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE Platform Note: Because some mobile device operating systems do not have current directory functionality, this method is not supported. For more information, see How to: Get the Application Directory.

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft