Visual C# Developer Center >
Visual C# Forums
>
Visual C# General
>
Seperating extension from it`s file name
Seperating extension from it`s file name
- I would like to seperate the file into two(2) peaces :1. to file name2. to file`s extensionI have a code, which does pretty well:
string path = treeView1.SelectedNode.FullPath; string fileName = listView1.FocusedItem.Text; string[] SeperateFile = fileName.Split(new char[] { '.' }); //then I call then in textBoxes for example: textBoxFileName.Text = SeperateFile[0]; textBoxExtension.Text = SeperateFile[1];
But the problem appears if the file has more then 1 dot. I would like to kno if it`s possible to do the code which will look :- for the file name - to the 1st dot- for the extension - after the last dotbig thanks
Answers
- Just use the following code to get the file extension and filename without extension (...these functions are already available in .net)
textBoxFileName.Text = IO.Path.GetFileNameWithoutExtension(fileName) textBoxExtension.Text = IO.Path.GetExtension(fileName)
Balaji Baskar [Please mark the post as answer if it answers your question]- Unmarked As Answer byMitja Bonca Wednesday, November 04, 2009 1:24 PM
- Marked As Answer byMitja Bonca Wednesday, November 04, 2009 8:38 AM
- Marked As Answer byMitja Bonca Wednesday, November 04, 2009 1:31 PM
- Proposed As Answer byReed Copsey, Jr. Wednesday, November 04, 2009 1:28 AM
- Use string.Replace.
textBoxExtension.Text = IO.Path.GetExtension(fileName).Replace(".", "");
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- Marked As Answer byMitja Bonca Wednesday, November 04, 2009 1:31 PM
All Replies
- Just use the following code to get the file extension and filename without extension (...these functions are already available in .net)
textBoxFileName.Text = IO.Path.GetFileNameWithoutExtension(fileName) textBoxExtension.Text = IO.Path.GetExtension(fileName)
Balaji Baskar [Please mark the post as answer if it answers your question]- Unmarked As Answer byMitja Bonca Wednesday, November 04, 2009 1:24 PM
- Marked As Answer byMitja Bonca Wednesday, November 04, 2009 8:38 AM
- Marked As Answer byMitja Bonca Wednesday, November 04, 2009 1:31 PM
- Proposed As Answer byReed Copsey, Jr. Wednesday, November 04, 2009 1:28 AM
textBoxFileName.Text = Path.GetFileNameWithoutExtension(stringThatHasPathFilenameExtention); textBoxExtension.Text = Path.GetExtension(stringThatHasPathFilenameExtention);
Ron Whittle - If the post is helpful or answers your question, please mark it as such. Not As Brightly Lit- On small problem:
textBoxExtension.Text = IO.Path.GetExtension(fileName)
I still have a dot infront of the extension (like: ".pdf") I would like to get rid of that dot. How? - Use string.Replace.
textBoxExtension.Text = IO.Path.GetExtension(fileName).Replace(".", "");
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- Marked As Answer byMitja Bonca Wednesday, November 04, 2009 1:31 PM
- :) you`re the man David. You know all.thanks


