2011-08-03 17 views
172

Programuję w WPF C#. Mam np. następującą ścieżkę:Pobierz nazwę pliku z ciągu znaków w C#

C:\Program Files\hello.txt 

i chcę wyjścia "cześć" od niego.

Ścieżka jest wyciągiem z bazy danych. Obecnie używam następujący sposób (Split ze ścieżką przez „\” a następnie ponownie podzielone przez „”):

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

To działa, ale uważam, że nie powinno być krótsze i bardziej inteligentne rozwiązanie do tego. Dowolny pomysł?

+0

W moim systemie, 'Path.GetFileName ("C: \\ \\ dev jakąś ścieżkę \\ \\ \\ file.cs do")' zwraca ten sam ciąg i nie konwertowanie go do "file.cs" z jakiegoś powodu. Jeśli skopiuję/wkleję mój kod do kompilatora online (np. Http://rextester.com/), działa ...? – jbyrd

Odpowiedz

8

Spróbuj tego:

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

Spowoduje to wyświetlenie "witaj" dla fileName.

20

spróbować

System.IO.Path.GetFileNameWithoutExtension(path); 

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, ponieważ może to być pomocne w przypadku, w którym działa to jako kopia zapasowa, w której nazwa pliku zawiera nieprawidłowe znaki [<, > itp. W ścieżce Path.GetInvalidChars()]. – bhuvin

4

Spróbuj tego,

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

Więc dokładnie tak, jak mówi najwyższa głosowana odpowiedź? – CodeCaster

+0

Użyłeś dokładnie tych samych metod, o których wspomniano w odpowiedziach z najwyższą liczbą głosów. – 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> 
Powiązane problemy