2011-08-03 83 views
172

Chương trình I trong WPF C#. Tôi có ví dụ con đường sau:Lấy tên tệp từ chuỗi đường dẫn trong C#

C:\Program Files\hello.txt 

và tôi muốn đầu ra "chào" từ nó.

Đường dẫn là một trích xuất chuỗi từ cơ sở dữ liệu. Hiện nay tôi đang sử dụng các phương pháp sau đây (tách ra từ con đường bằng '\' sau đó chia lại bằng một ''):

string path = "C:\\Program Files\\hello.txt"; 
string[] pathArr = path.Split('\\'); 
string[] fileArr = pathArr.Last().Split('.'); 
string fileName = fileArr.Last().ToString(); 

Nó hoạt động, nhưng tôi tin rằng cần có giải pháp ngắn hơn và thông minh hơn để đó. Bất kỳ ý tưởng?

+0

Trong hệ thống của tôi, 'Path.GetFileName ("C: \\ \\ dev một số \\ \\ con đường để \\ file.cs")' đang trở lại cùng một chuỗi và không chuyển đổi nó thành "file.cs" vì một số lý do. Nếu tôi sao chép/dán mã của tôi vào một trình biên dịch trực tuyến (như http://rextester.com/), nó hoạt động ...? – jbyrd

Trả lời

8

Hãy thử điều này:

string fileName = Path.GetFileNameWithoutExtension(@"C:\Program Files\hello.txt"); 

Điều này sẽ trả về "hello" cho tên tệp.

20

thử

System.IO.Path.GetFileNameWithoutExtension(path); 

bản demo

string fileName = @"C:\mydir\myfile.ext"; 
string path = @"C:\mydir\"; 
string result; 

result = Path.GetFileNameWithoutExtension(fileName); 
Console.WriteLine("GetFileNameWithoutExtension('{0}') returns '{1}'", 
    fileName, result); 

result = Path.GetFileName(path); 
Console.WriteLine("GetFileName('{0}') returns '{1}'", 
    path, result); 

// This code produces output similar to the following: 
// 
// GetFileNameWithoutExtension('C:\mydir\myfile.ext') returns 'myfile' 
// GetFileName('C:\mydir\') returns '' 

https://msdn.microsoft.com/en-gb/library/system.io.path.getfilenamewithoutextension%28v=vs.80%29.aspx

4
string Location = "C:\\Program Files\\hello.txt"; 

string FileName = Location.Substring(Location.LastIndexOf('\\') + 
    1); 
+0

+1 vì điều này có thể hữu ích trong trường hợp nó hoạt động như một Sao lưu trong đó tên tệp chứa các ký tự không hợp lệ [<, > vv trong Path.GetInvalidChars()]. – bhuvin

4

Hãy thử điều này,

string [email protected]"C:\mydir\myfile.ext"; 
string Result=Path.GetFileName(FilePath);//With Extension 
string Result=Path.GetFileNameWithoutExtension(FilePath);//Without Extension 
+0

Vì vậy, chính xác như câu trả lời bình chọn cao nhất nói? – CodeCaster

+0

Bạn đã sử dụng chính xác cùng một phương pháp như được đề cập trong câu trả lời bình chọn cao nhất. – CodeCaster

0
Namespace: using System.IO; 
//use this to get file name dynamically 
string filelocation = Properties.Settings.Default.Filelocation; 
//use this to get file name statically 
//string filelocation = @"D:\FileDirectory\"; 
string[] filesname = Directory.GetFiles(filelocation); //for multiple files 

Your path configuration in App.config file if you are going to get file name dynamically - 

    <userSettings> 
     <ConsoleApplication13.Properties.Settings> 
      <setting name="Filelocation" serializeAs="String"> 
      <value>D:\\DeleteFileTest</value> 
      </setting> 
       </ConsoleApplication13.Properties.Settings> 
     </userSettings> 
Các vấn đề liên quan