Share c# get file name from path

manhhung299

New member
## C# Nhận tên tệp từ đường dẫn

### 1. Nhận tên tệp từ một đường dẫn đầy đủ

Để lấy tên tệp từ một đường dẫn đầy đủ trong C#, bạn có thể sử dụng phương thức `path.getFileName ()`.Phương thức này đưa đường dẫn đầy đủ đến một tệp làm đầu vào của nó và trả về tên tệp làm đầu ra của nó.Ví dụ: mã sau có tên tệp từ đường dẫn `" C: \ user \ user \ Documents \ file.txt "`:

`` `C#
String fileName = path.getFileName ("C: \ user \ user \ Documents \ file.txt");
`` `

### 2. Nhận tên tệp mà không có phần mở rộng

Để lấy tên tệp mà không có phần mở rộng, bạn có thể sử dụng phương thức `path.getFileNamewithOutExtension ()`.Phương thức này sẽ đưa đường dẫn đầy đủ đến một tệp làm đầu vào của nó và trả về tên tệp mà không có phần mở rộng làm đầu ra của nó.Ví dụ: mã sau có tên tệp mà không có phần mở rộng từ đường dẫn `" C: \ user \ user \ Documents \ file.txt "`::

`` `C#
Chuỗi FILENAMEWITHOUTEXTENTS = path.getFileNamewithOutExtension ("C: \ user \ user \ Documents \ file.txt");
`` `

### 3. Lấy tên thư mục từ một đường dẫn

Để lấy tên thư mục từ một đường dẫn, bạn có thể sử dụng phương thức `path.getDirectoryName ()`.Phương thức này đưa đường dẫn đầy đủ đến một tệp làm đầu vào của nó và trả về tên thư mục làm đầu ra của nó.Ví dụ: mã sau có tên thư mục từ đường dẫn `" C: \ user \ user \ Documents \ file.txt "`:

`` `C#
Chuỗi thư mụcName = path.getDirectoryName ("C: \ user \ user \ Documents \ file.txt");
`` `

### 4. Lấy tên thư mục cha mẹ từ một đường dẫn

Để lấy tên thư mục cha từ một đường dẫn, bạn có thể sử dụng phương thức `path.getParent ()`.Phương thức này sẽ đưa đường dẫn đầy đủ đến một tệp làm đầu vào của nó và trả về tên thư mục cha làm đầu ra của nó.Ví dụ: mã sau có tên thư mục cha từ đường dẫn `" C: \ user \ user \ Documents \ file.txt "`::

`` `C#
Chuỗi ParentDirectoryName = path.getParent ("C: \ user \ user \ Documents \ file.txt");
`` `

### 5. Nhận tiện ích mở rộng từ tên tệp

Để nhận tiện ích mở rộng từ tên tệp, bạn có thể sử dụng phương thức `path.getextension ()`.Phương thức này lấy tên tệp làm đầu vào của nó và trả về phần mở rộng làm đầu ra của nó.Ví dụ: mã sau nhận được phần mở rộng từ tên tệp `" file.txt "`:

`` `C#
chuỗi mở rộng = path.getextension ("file.txt");
`` `

## hashtags

* #C#
* #tài liệu
* #Con đường
* #Programming
* #tutorial
=======================================
## C# Get File Name from Path

### 1. Get the file name from a full path

To get the file name from a full path in C#, you can use the `Path.GetFileName()` method. This method takes the full path to a file as its input and returns the file name as its output. For example, the following code gets the file name from the path `"C:\Users\user\Documents\file.txt"`:

```c#
string fileName = Path.GetFileName("C:\Users\user\Documents\file.txt");
```

### 2. Get the file name without the extension

To get the file name without the extension, you can use the `Path.GetFileNameWithoutExtension()` method. This method takes the full path to a file as its input and returns the file name without the extension as its output. For example, the following code gets the file name without the extension from the path `"C:\Users\user\Documents\file.txt"`:

```c#
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension("C:\Users\user\Documents\file.txt");
```

### 3. Get the directory name from a path

To get the directory name from a path, you can use the `Path.GetDirectoryName()` method. This method takes the full path to a file as its input and returns the directory name as its output. For example, the following code gets the directory name from the path `"C:\Users\user\Documents\file.txt"`:

```c#
string directoryName = Path.GetDirectoryName("C:\Users\user\Documents\file.txt");
```

### 4. Get the parent directory name from a path

To get the parent directory name from a path, you can use the `Path.GetParent()` method. This method takes the full path to a file as its input and returns the parent directory name as its output. For example, the following code gets the parent directory name from the path `"C:\Users\user\Documents\file.txt"`:

```c#
string parentDirectoryName = Path.GetParent("C:\Users\user\Documents\file.txt");
```

### 5. Get the extension from a file name

To get the extension from a file name, you can use the `Path.GetExtension()` method. This method takes the file name as its input and returns the extension as its output. For example, the following code gets the extension from the file name `"file.txt"`:

```c#
string extension = Path.GetExtension("file.txt");
```

## Hashtags

* #C#
* #file
* #Path
* #Programming
* #tutorial
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top