Directory.GetCurrentDirectory Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Získá aktuální pracovní adresář aplikace.
public:
static System::String ^ GetCurrentDirectory();
public static string GetCurrentDirectory ();
static member GetCurrentDirectory : unit -> string
Public Shared Function GetCurrentDirectory () As String
Návraty
Řetězec, který obsahuje absolutní cestu aktuálního pracovního adresáře a nekončí zpětným lomítkem (\).
Výjimky
Volající nemá požadované oprávnění.
Operační systém je systém Windows CE, který nemá aktuální funkce adresáře.
Tato metoda je dostupná v rozhraní .NET Compact Framework, ale v současné době není podporována.
Příklady
Následující příklad ukazuje, jak použít metodu GetCurrentDirectory
.
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
Poznámky
Aktuální adresář se liší od původního adresáře, což je ten, ze kterého se proces spustil.
Seznam běžných vstupně-výstupních úloh najdete v tématu Běžné vstupně-výstupní úlohy.