Computer >> Máy Tính >  >> Lập trình >> C#

Phương thức đường dẫn trong C #

Để xử lý Đường dẫn tệp trong C #, hãy sử dụng các phương pháp Đường dẫn. Các phương thức này nằm trong Không gian tên System.IO.

Một số trong số đó là -

GetExtension

Truy xuất phần mở rộng của tệp bằng phương thức GetExtension ().

Ví dụ:.txt, .dat, v.v.

GetFileName

Lấy tên của tệp bằng phương thức GetFileName ().

Ví dụ:new.txt, details.dat, v.v.

GetFileNameWithoutExtension

Truy xuất tên tệp không có phần mở rộng bằng phương thức GetFileNameWithoutExtension ().

Ví dụ:mới, chi tiết, v.v.

Hãy để chúng tôi xem một ví dụ -

Ví dụ

using System.IO;
using System;
class Program {
   static void Main() {
      string myPath = "D:\\one.txt";
      string fileExtension = Path.GetExtension(myPath);
      string fileName = Path.GetFileName(myPath);
      string noExtension = Path.GetFileNameWithoutExtension(myPath);

      Console.WriteLine("File Extension: "+fileExtension);
      Console.WriteLine("Fine Name: "+fileName);
      Console.WriteLine("File Name without extension: "+noExtension);
   }
}

Đầu ra

File Extension: .txt
Fine Name: D:\one.txt
File Name without extension: D:\one