Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Heres my code.
 
DirectoryInfo di = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.SendTo));
 
Foreach(fileinfo fi in di.getfiles())
{
ToolstripCombobox1.items.add(fi.name);
}
Thats it. As the title indicates, i want to add all the files in that folder to the combobox, but without the file extension.
 
Thanks.
Posted 19-May-12 3:26am
Edited 19-May-12 4:01am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You need this: http://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension[^]
ToolstripCombobox1.items.add(Path.GetFileNameWithoutExtension(fi.name));
  Permalink  
v2
Comments
Raidzen10 at 19-May-12 9:59am
   
Thank you. Works too. But prefer above other solution.
Zoltán Zörgő at 19-May-12 14:36pm
   
Well... you can use what you want, this was what you should have used...
VJ Reddy at 20-May-12 9:49am
   
Good use of Path. 5!
Zoltán Zörgő at 21-May-12 0:01am
   
Thank you.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

IT might help you,
 
private void PopulateToolStripComboBox()
{
    DirectoryInfo di = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.SendTo));
    toolStripComboBox1.Items.AddRange(di.GetFiles().Select(file => Path.GetFileNameWithoutExtension(file.Name)).ToArray());
}
 
For further ref,
 
List.AddRange[^]
How does it work in C#? - Part 3 (C# Linq in detail)[^]
 
Hope it helps Smile | :)
  Permalink  
Comments
VJ Reddy at 20-May-12 9:47am
   
Good use of LINQ and Path 5!
Mohammad A Rahman at 20-May-12 9:49am
   
Thanks VJ :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Use GetFileNameWithoutExtension. No substrings needed Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Easy:
string name = fi.Name;
name = name.Substring(0, name.Length - fi.Extension.Length);
  Permalink  
Comments
Raidzen10 at 19-May-12 9:58am
   
Thanx.
Clifford Nelson at 19-May-12 10:54am
   
This is not a good solution, it is recommended that you use the Path.GetFileNameWithoutExtension
Shahin Khorshidnia at 19-May-12 12:32pm
   
Good solution my +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 534
1 Gihan Liyanage 298
2 ChintanShukla 295
3 RyanDev 250
4 Richard Deeming 250
0 Sergey Alexandrovich Kryukov 8,906
1 OriginalGriff 7,771
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 19 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100