943,645 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 17404
  • C# RSS
Jan 5th, 2009
0

Getting File Extension

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
gallian99 is offline Offline
77 posts
since Jan 2009
Jan 5th, 2009
0

Re: Getting File Extension

Say L is the last character in your filename.(Same as Fname.Length-1)
Then Fname.Substring(L-3,L); should do the trick.
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is online now Online
3,736 posts
since Oct 2008
Jan 5th, 2009
1

Re: Getting File Extension

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() ?

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 155
Solved Threads: 41
Posting Whiz in Training
rapture is offline Offline
294 posts
since Jul 2007
Jan 5th, 2009
0

Re: Getting File Extension

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.
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Jan 5th, 2009
0

Re: Getting File Extension

*NEVER* make the assumption that a files extension is only 3 characters.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Jan 8th, 2009
0

Re: Getting File Extension

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;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lhavana is offline Offline
1 posts
since Jan 2009
Jun 29th, 2010
0
Re: Getting File Extension
System.IO.FileInfo fi = new System.IO.FileInfo(FileName);
string ext = fi.Extension.ToString();
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kia_mls is offline Offline
1 posts
since Jun 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C# Forum Timeline: How Do I define a regex pattern for this??
Next Thread in C# Forum Timeline: XML Parsing





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC