Directory.GetCurrentDirectory Метод
Важно!
Некоторые сведения относятся к предварительной версии продукта, в которую до выпуска могут быть внесены существенные изменения. Майкрософт не предоставляет никаких гарантий, явных или подразумеваемых, относительно приведенных здесь сведений.
Получает текущий рабочий каталог приложения.
public:
static System::String ^ GetCurrentDirectory();
public static string GetCurrentDirectory ();
static member GetCurrentDirectory : unit -> string
Public Shared Function GetCurrentDirectory () As String
Строка, содержащая абсолютный путь к текущему рабочему каталогу и не заканчивающаяся обратной косой чертой (\).
У вызывающего объекта отсутствует необходимое разрешение.
Операционной системой является Windows CE, которая не поддерживает функциональность текущего каталога.
Этот метод доступен в .NET Compact Framework, но в настоящее время не поддерживается.
В следующем примере показано использование метода 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());
}
}
}
open System
open System.IO
try
// Get the current directory.
let path = Directory.GetCurrentDirectory()
let target = @"c:\temp"
printfn $"The current directory is {path}"
if not (Directory.Exists target) then
Directory.CreateDirectory target |> ignore
// Change the current directory.
Environment.CurrentDirectory <- target
if path.Equals(Directory.GetCurrentDirectory()) then
printfn "You are in the temp directory."
else
printfn "You are not in the temp directory."
with e ->
printfn $"The process failed: {e}"
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
Текущий каталог отличается от исходного каталога, из которого был запущен процесс.
Список распространенных задач ввода-вывода см. в разделе Общие задачи ввода-вывода.
Продукт | Версии |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
Отзыв о .NET
.NET — это проект с открытым исходным кодом. Выберите ссылку, чтобы оставить отзыв: