Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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 (\).

Exception Condition
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.

The following example demonstrates how to use the GetCurrentDirectory method.

using namespace System;
using namespace System::IO;
int 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 );
   }

}

FileIOPermission

for access to path information for the current directory. Associated enumeration: FileIOPermissionAccess::PathDiscovery

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
Show:
© 2017 Microsoft