DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C# (http://www.daniweb.com/forums/forum61.html)
-   -   Getting File Extension (http://www.daniweb.com/forums/thread166320.html)

gallian99 Jan 5th, 2009 5:16 pm
Getting File Extension
 
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

ddanbe Jan 5th, 2009 5:29 pm
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.

rapture Jan 5th, 2009 5:42 pm
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() ?

 
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.)

Ramy Mahrous Jan 5th, 2009 7:10 pm
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.

LizR Jan 5th, 2009 7:36 pm
Re: Getting File Extension
 
*NEVER* make the assumption that a files extension is only 3 characters.

lhavana Jan 8th, 2009 4:15 am
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;
}


All times are GMT -4. The time now is 12:51 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC