Directory.GetCurrentDirectory 方法

获取应用程序的当前工作目录。

**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Shared Function GetCurrentDirectory As String
用法
Dim returnValue As String

returnValue = Directory.GetCurrentDirectory
public static string GetCurrentDirectory ()
public:
static String^ GetCurrentDirectory ()
public static String GetCurrentDirectory ()
public static function GetCurrentDirectory () : String

返回值

包含当前工作目录的路径的字符串。

异常

异常类型 条件

UnauthorizedAccessException

调用方没有所要求的权限。

NotSupportedException

操作系统为 Windows CE,该系统不具有当前目录功能。

此方法在 .NET Compact Framework 中可用,但是当前并不支持。

备注

当前目录不同于原始目录,后者是从其开始进程的目录。

下表列出了其他典型或相关的 I/O 任务的示例。

若要执行此操作...

请参见本主题中的示例...

创建文本文件。

如何:向文件写入文本

写入文本文件。

如何:向文件写入文本

读取文本文件。

如何:从文件读取文本

查看目录中的文件。

Name

查看目录的子目录。

GetDirectories

GetDirectories

查看目录的所有子目录中的所有文件。

GetFileSystemInfos

查看目录大小。

Directory

确定文件是否存在。

Exists

确定目录是否存在。

Exists

Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE 平台说明: 因为某些移动设备操作系统不具有当前目录功能,所以不支持此方法。

示例

下面的代码示例说明了 GetCurrentDirectory 方法。

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

}
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

.NET Framework 安全性

平台

Windows 98、Windows 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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Directory 类
Directory 成员
System.IO 命名空间

其他资源

文件和流 I/O
如何:从文件读取文本
如何:向文件写入文本