Directory.GetCurrentDirectory Method
.NET Framework 3.0
Gets the current working directory of the application.
Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Assembly: mscorlib (in mscorlib.dll)
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
- FileIOPermission for access to path information for the current directory. Associated enumeration: FileIOPermissionAccess.PathDiscovery
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.