Directory.GetCurrentDirectory Directory.GetCurrentDirectory Directory.GetCurrentDirectory Directory.GetCurrentDirectory Method

定義

取得應用程式的目前工作目錄。Gets the current working directory of the application.

public:
 static System::String ^ GetCurrentDirectory();
public static string GetCurrentDirectory ();
static member GetCurrentDirectory : unit -> string
Public Shared Function GetCurrentDirectory () As String

傳回

包含目前工作目錄完整路徑且未以反斜線 (\) 結尾的字串。A string that contains the absolute path of the current working directory, and does not end with a backslash (\).

例外狀況

呼叫端沒有必要的權限。The caller does not have the required permission.

作業系統是 Windows CE,它沒有當前的目錄功能。The operating system is Windows CE, which does not have current directory functionality.

.NET Compact Framework 提供方法,但目前不支援。This method is available in the .NET Compact Framework, but is not currently supported.

範例

下列範例示範如何使用 GetCurrentDirectory 方法。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 );
   }

}

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

備註

目前的目錄是不同於原始的目錄,也就是從中啟動程序。The current directory is distinct from the original directory, which is the one from which the process was started.

如需一般 I/O 工作的清單,請參閱 < 一般 I/O 工作For a list of common I/O tasks, see Common I/O Tasks.

安全性

FileIOPermission
目前的目錄路徑資訊存取。for access to path information for the current directory. 相關聯的列舉型別: PathDiscoveryAssociated enumeration: PathDiscovery

適用於

另請參閱