Getting File Extension

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2009
Posts: 68
Reputation: gallian99 is an unknown quantity at this point 
Solved Threads: 0
gallian99 gallian99 is offline Offline
Junior Poster in Training

Getting File Extension

 
0
  #1
Jan 5th, 2009
I've been trying to use substring to get the last three digits as part of the extension but wasn't successful.

Can anyone tell me how I can extract the extension from the file name?

Rgs
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,908
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 274
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Getting File Extension

 
0
  #2
Jan 5th, 2009
Say L is the last character in your filename.(Same as Fname.Length-1)
Then Fname.Substring(L-3,L); should do the trick.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 276
Reputation: rapture has a spectacular aura about rapture has a spectacular aura about 
Solved Threads: 37
rapture rapture is offline Offline
Posting Whiz in Training

Re: Getting File Extension

 
1
  #3
Jan 5th, 2009
You don't mention if you have to use substring to get the extension or if that is just the method to grab it you decided to use so I'll ask another question . . .

Is there any reason you are not using path.getextension() ?

  1.  
  2. extension = Path.GetExtension(fileName);

You can get this info from a google search on C# path.getextension or msdn is here: http://msdn.microsoft.com/en-us/libr...extension.aspx

(wondering because .aspx is four while .doc is three etc.)
Last edited by rapture; Jan 5th, 2009 at 5:42 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Getting File Extension

 
0
  #4
Jan 5th, 2009
After the last "." get the string is after
you may have file, its name readme.program.user.txt!!
It's the time to play with string class, you can also use regular expression and you can use FileInfo.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Getting File Extension

 
0
  #5
Jan 5th, 2009
*NEVER* make the assumption that a files extension is only 3 characters.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 1
Reputation: lhavana is an unknown quantity at this point 
Solved Threads: 0
lhavana lhavana is offline Offline
Newbie Poster

Re: Getting File Extension

 
0
  #6
Jan 8th, 2009
try this simple procedure :

private string Getextension(string fileName)
{
string ext = string.Empty;
Boolean hita = false;
int i = fileName.Length - 1;
char[] arr = fileName.ToCharArray();
while(i>0 & !hita)
{
if (arr[i] == '.') hita = true;
else ext = arr[i] + ext;
i = i - 1;
}

return ext;
}
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC