Directory.GetCurrentDirectory Método

Definición

Obtiene el directorio de trabajo actual de la aplicación.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

Devoluciones

Cadena que contiene la ruta de acceso absoluta del directorio de trabajo actual y no finaliza con una barra diagonal inversa (\).A string that contains the absolute path of the current working directory, and does not end with a backslash (\).

Excepciones

El llamador no dispone del permiso requerido.The caller does not have the required permission.

El sistema operativo es Windows CE, que no tiene la funcionalidad de directorio actual.The operating system is Windows CE, which does not have current directory functionality.

Este método está disponible en .NET Compact Framework, pero no se admite actualmente.This method is available in the .NET Compact Framework, but is not currently supported.

Ejemplos

En el siguiente ejemplo se muestra cómo se utiliza el método 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.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

Comentarios

El directorio actual es distinto del directorio original, que es el en el que se inició el proceso.The current directory is distinct from the original directory, which is the one from which the process was started.

Para obtener una lista de tareas de e/s comunes, consulte tareas comunes de e/s.For a list of common I/O tasks, see Common I/O Tasks.

Seguridad

FileIOPermission
para acceder a la información de la ruta de acceso del directorio actual.for access to path information for the current directory. Enumeración asociada: PathDiscoveryAssociated enumeration: PathDiscovery

Se aplica a

Consulte también: