Share c# get current directory

lycoco

New member
#C ##Get thư mục hiện tại #directory #.net #Programming ## 1.Nhận thư mục hiện tại trong C#

`Thư mục.getCienDirectory ()` Phương thức trả về đường dẫn của thư mục hiện tại.Thư mục hiện tại là thư mục chứa tệp hiện đang được thực thi.

Để sử dụng phương thức `Directory.getCurrentDirectory ()`, bạn có thể sử dụng mã sau:

`` `C#
chuỗi currentDirectory = thư mục.getCienDirectory ();
`` `

## 2.Nhận thư mục hiện tại bằng cách sử dụng thuộc tính `Môi trường.CurrentDirectory`

Thuộc tính `Môi trường.CurrentDirectory` cũng trả về đường dẫn của thư mục hiện tại.Tuy nhiên, thuộc tính `Môi trường.Thuộc tính `Môi trường.CurrentDirectory` có thể trả về một giá trị khác nhau tùy thuộc vào môi trường mà mã đang chạy.

Để sử dụng thuộc tính `Môi trường.CurrentDirectory`, bạn có thể sử dụng mã sau:

`` `C#
chuỗi currentDirectory = môi trường.currentDirectory;
`` `

## 3.Nhận thư mục hiện tại bằng cách sử dụng phương thức `system.io.directory.getDirectoryName ()`

`System.io.Directory.GetDirectoryName ()` Phương thức trả về đường dẫn của thư mục chứa tệp hoặc thư mục được chỉ định.Bạn có thể sử dụng phương thức `system.io.directory.getDirectoryName ()` để lấy đường dẫn của thư mục hiện tại bằng cách chuyển đường dẫn của tệp hiện tại đến phương thức.

Để sử dụng `system.io.directory.getDirectoryName ()`, bạn có thể sử dụng mã sau:

`` `C#
Chuỗi hiện tạiDirectory = System.io.Directory.GetDirectoryName (System.io.path.getDirectoryName (__ File__));
`` `

##4.Ví dụ

Mã sau đây hiển thị một ví dụ về cách lấy thư mục hiện tại trong C#:

`` `C#
// Nhận thư mục hiện tại bằng phương thức thư mục.getCienDirectory ().
chuỗi currentDirectory = thư mục.getCienDirectory ();

// Nhận thư mục hiện tại bằng cách sử dụng thuộc tính Môi trường.CurrentDirectory.
chuỗi currentDirectory2 = môi trường.currentDirectory;

// Nhận thư mục hiện tại bằng phương thức System.io.Directory.GetDirectoryName ().
chuỗi currentDirectory3 = System.io.Directory.GetDirectoryName (System.io.path.getDirectoryName (__ File__));

// In thư mục hiện tại vào bảng điều khiển.
Console.WriteLine ("Thư mục hiện tại là: {0}", currentDirectory);
Console.WriteLine ("Thư mục hiện tại là: {0}", currentDirectory2);
Console.WriteLine ("Thư mục hiện tại là: {0}", currentDirectory3);
`` `

## 5.Phần kết luận

Trong bài viết này, bạn đã học cách lấy thư mục hiện tại trong C#.Bạn đã học cách sử dụng thư mục.getCienDirectory () `Phương thức, thuộc tính` Môi trường

## hashtags

* #C#
* #Get thư mục hiện tại
* #Danh mục
* #.MẠNG LƯỚI
* #Programming
=======================================
#C# #Get Current Directory #directory #.net #Programming ##1. Get the current directory in C#

The `Directory.GetCurrentDirectory()` method returns the path of the current directory. The current directory is the directory that contains the file that is currently being executed.

To use the `Directory.GetCurrentDirectory()` method, you can use the following code:

```c#
string currentDirectory = Directory.GetCurrentDirectory();
```

##2. Get the current directory using the `Environment.CurrentDirectory` property

The `Environment.CurrentDirectory` property also returns the path of the current directory. However, the `Environment.CurrentDirectory` property is not as reliable as the `Directory.GetCurrentDirectory()` method. The `Environment.CurrentDirectory` property can return a different value depending on the environment in which the code is running.

To use the `Environment.CurrentDirectory` property, you can use the following code:

```c#
string currentDirectory = Environment.CurrentDirectory;
```

##3. Get the current directory using the `System.IO.Directory.GetDirectoryName()` method

The `System.IO.Directory.GetDirectoryName()` method returns the path of the directory that contains the specified file or directory. You can use the `System.IO.Directory.GetDirectoryName()` method to get the path of the current directory by passing the current file's path to the method.

To use the `System.IO.Directory.GetDirectoryName()` method, you can use the following code:

```c#
string currentDirectory = System.IO.Directory.GetDirectoryName(System.IO.Path.GetDirectoryName(__FILE__));
```

##4. Example

The following code shows an example of how to get the current directory in C#:

```c#
// Get the current directory using the Directory.GetCurrentDirectory() method.
string currentDirectory = Directory.GetCurrentDirectory();

// Get the current directory using the Environment.CurrentDirectory property.
string currentDirectory2 = Environment.CurrentDirectory;

// Get the current directory using the System.IO.Directory.GetDirectoryName() method.
string currentDirectory3 = System.IO.Directory.GetDirectoryName(System.IO.Path.GetDirectoryName(__FILE__));

// Print the current directory to the console.
Console.WriteLine("The current directory is: {0}", currentDirectory);
Console.WriteLine("The current directory is: {0}", currentDirectory2);
Console.WriteLine("The current directory is: {0}", currentDirectory3);
```

##5. Conclusion

In this article, you learned how to get the current directory in C#. You learned how to use the `Directory.GetCurrentDirectory()` method, the `Environment.CurrentDirectory` property, and the `System.IO.Directory.GetDirectoryName()` method.

##Hashtags

* #C#
* #Get Current Directory
* #directory
* #.net
* #Programming
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top