C # Remove file extension from string list

What my program does is basically listing the file names (including the extension) from directory to list. Then it has a sort function that sorts the list lines in alphabetical order.

Finally, it has a binary search function that allows users to enter any line that the program will compare and display the matched results in a list.

Now all these functions work fine, but I can’t remove the extension after the file name after searching.

For example, the scan and sort part lists file names like: filename.mp3

Now, what I want to do, when the search button is pressed, I need to remove the file extension and display only the file name.

private void buttonSearch_Click(object sender, RoutedEventArgs e) { listBox1.Items.Clear(); string searchString = textBoxSearchPath.Text; int index = BinarySearch(list1, 0, list1.Count, searchString); for (int n = index; n < list1.Count; n++) { //Removes file extension from last decimal point ''not working'' int i = list1[n].LastIndexOf("."); if (i > 0) list1[n].Substring(0, i); // Adds items to list if (list1[n].IndexOf(searchString, StringComparison.OrdinalIgnoreCase) != 0) break; listBox1.Items.Add(list1[n]); } MessageBox.Show("Done"); } 
+4
source share
7 answers

The Substring method returns a new fresh copy of the string copied from the original one. If you want to "cut the extension", then you should get what the substring returns and store somewhere, that is:

 int i = list1[n].LastIndexOf("."); if (i > 0) list1[n] = list1[n].Substring(0, i); 

However, this is a rather strange way to remove an extension.

Firstly, using Substring(0,idx) is odd, since there is Remove(idx) (link) , which does just that:

 int i = list1[n].LastIndexOf("."); if (i > 0) list1[n] = list1[n].Remove(i); 

But, sencondly, there is an even better way to do this: the System.IO.Path class provides you with a set of well-written static methods that, for example, remove the extension (editing: this is what L-Three suggests in the comments), with full point processing etc.:

 var str = System.IO.Path.GetFileNameWithoutExtension("myfile.txt"); // == "myfile" 

See MSDN link

It still returns a copy, and you still need to save the result somewhere!

 list1[n] = Path.GetFileNameWithoutExtension( list1[n] ); 
+2
source

C # is so simple that if something takes more than 2 minutes, there is probably a method for doing this in the Framework.

+5
source

Try as below, this will help you ....

Description: File name without extension

  listBox1.Items.Add(Path.GetFileNameWithoutExtension(list1[n])); 
+3
source
+2
source

Use the Path.GetFileNameWithoutExtension method. Pretty easy, I think.

http://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension.aspx

+2
source

You don’t know how you implemented a search in your directory, but you can use LINQ to your advantage in these situations for clean, easy to read code:

 var files = Directory.EnumerateFiles(@"\\PathToFiles") .Select(f => Path.GetFileNameWithoutExtension(f)); 

If you are using .NET 4.0, listing files seems to be an excellent choice compared to GetFiles. However, it also looks like you want to get both the full path to the file and the file name without the extension. Here's how you could create a dictionary to remove a loop through a collection twice:

 var files = Directory.EnumerateFiles(@"\\PathToFiles") .ToDictionary(f => f, n => Path.GetFileNameWithoutExtension(n)); 
+1
source

The way to do this is if you don't have a file path, just the file name

 string filePath = (@"D:/" + fileName); string withoutExtension = Path.getFileNameWithoutExtension(filePath); 
0
source

All Articles