Export (0) Print
Expand All
525 out of 533 rated this helpful - Rate this topic

Directory.GetCurrentDirectory Method

Gets the current working directory of the application.

[Visual Basic]
Public Shared Function GetCurrentDirectory() As String
[C#]
public static string GetCurrentDirectory();
[C++]
public: static String* GetCurrentDirectory();
[JScript]
public static function GetCurrentDirectory() : String;

Return Value

A string containing the path of the current working directory.

Exceptions

Exception Type Condition
UnauthorizedAccessException The caller does not have the required permission.

Remarks

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

For an example of using this method, see the Example section below. The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
See the files in a directory. Name
See the subdirectories of a directory. GetDirectories

GetDirectories

See all the files in all subdirectories of a directory. GetFileSystemInfos
Find the size of a directory. Directory
Determine if a file exists. Exists
Determine if a directory exists. Exists

.NET Compact Framework Platform Note:  The .NET Compact Framework does not support GetCurrentDirectory because current directory functionality is not used in devices running Windows CE .NET.

Example

[Visual Basic, C#, C++] The following example demonstrates the GetCurrentDirectory method.

[Visual Basic] 
Imports System
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        Try
            ' Get the current directory.
            Dim path As String = Directory.GetCurrentDirectory()
            Dim target As String = "c:\temp"
            Console.WriteLine("The current directory is {0}", path)
            If Directory.Exists(target) = False Then
                Directory.CreateDirectory(target)
            End If
            ' Change the current directory.
            Environment.CurrentDirectory = (target)
            If path.Equals(Directory.GetCurrentDirectory()) Then
                Console.WriteLine("You are in the temp directory.")
            Else
                Console.WriteLine("You are not in the temp directory.")
            End If
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

[C#] 
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());
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    try {
        // Get the current directory.
        String* path = Directory::GetCurrentDirectory();
        String* target = S"c:\\temp";
        Console::WriteLine(S"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(S"You are in the temp directory.");
        } else {
            Console::WriteLine(S"You are not in the temp directory.");
        }
    } catch (Exception* e) {
        Console::WriteLine(S"The process failed: {0}", e);
    }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

Directory Class | Directory Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2014 Microsoft. All rights reserved.