Export (0) Print
Expand All

Environment.CurrentDirectory Property

Gets and sets the fully qualified path of the current directory; that is, the directory from which this process starts.

[Visual Basic]
Public Shared Property CurrentDirectory As String
[C#]
public static string CurrentDirectory {get; set;}
[C++]
public: __property static String* get_CurrentDirectory();
public: __property static void set_CurrentDirectory(String*);
[JScript]
public static function get CurrentDirectory() : String;
public static function set CurrentDirectory(String);

Property Value

A string containing a directory path.

Exceptions

Exception Type Condition
ArgumentException Attempted to set to an empty string ("").
ArgumentNullException Attempted to set to a null reference (Nothing in Visual Basic).
IOException An I/O error occurred.
FileNotFoundException Attempted to set a local path that cannot be found.
DirectoryNotFoundException Attempted to set a universal naming convention (UNC) path that cannot be found.
SecurityException The caller does not have the appropriate permission.

Remarks

By definition, if this process starts in the root directory of a local or network drive, the value of this property is the drive name followed by a trailing slash (for example, "C:\"). If this process starts in a subdirectory, the value of this property is the drive and subdirectory path, without a trailing slash (for example, "C:\mySubDirectory").

Example

[Visual Basic, C#, C++] The following code example demonstrates setting the CurrentDirectory property.

[Visual Basic] 
' Change the directory to %WINDIR%                            
Environment.CurrentDirectory = Environment.GetEnvironmentVariable("windir")
Dim info As New DirectoryInfo(".")
Console.WriteLine(("Directory Info:   " + info.FullName))        

[C#] 
// Change the directory to %WINDIR%                            
Environment.CurrentDirectory = Environment.GetEnvironmentVariable("windir");        
DirectoryInfo info = new DirectoryInfo(".");
    lock(info)
    {      
    Console.WriteLine("Directory Info:   "+info.FullName);
    }

[C++] 
// Change the directory to %WINDIR%                            
Environment::CurrentDirectory = Environment::GetEnvironmentVariable(S"windir");        
DirectoryInfo* info = new DirectoryInfo(S".");
System::Threading::Monitor::Enter(info);
try {      
    Console::WriteLine(S"Directory Info:   {0}", info->FullName);
} __finally {
    System::Threading::Monitor::Exit(info);
}

[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 Framework Security: 

See Also

Environment Class | Environment Members | System Namespace

Show:
© 2014 Microsoft