Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Heres my code.

C#
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
Updated 30-Apr-16 7:32am
v3

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

C#
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<t>.AddRange[^]
How does it work in C#? - Part 3 (C# Linq in detail)[^]

Hope it helps :)
 
Share this answer
 
Comments
VJ Reddy 20-May-12 9:47am    
Good use of LINQ and Path 5!
Mohammad A Rahman 20-May-12 9:49am    
Thanks VJ :)
Use GetFileNameWithoutExtension. No substrings needed :)
 
Share this answer
 
Easy:
C#
string name = fi.Name;
name = name.Substring(0, name.Length - fi.Extension.Length);
 
Share this answer
 
Comments
Raidzen10 19-May-12 9:58am    
Thanx.
Clifford Nelson 19-May-12 10:54am    
This is not a good solution, it is recommended that you use the Path.GetFileNameWithoutExtension
Shahin Khorshidnia 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900