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
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
A few clarifications and a small, safe pattern to use.
You were right to try a substring, but manual index math is brittle — 's example illustrates that off-by-one mistakes happen and will break for short names. Also remember 's warning: extensions are not always three characters, and correctly calls out multiple dots inside names. The simplest, most reliable route is to use the framework helpers mentioned by and when you can, but if you must roll your own make it handle these edge cases.
Robust extraction rules to follow:
.gz from archive.tar.gz) or multi-part extensions (.tar.gz) and handle that explicitly.A compact, safe C# implementation:
private static string GetExtensionSafe(string pathOrName)
{
if (string.IsNullOrEmpty(pathOrName)) return string.Empty;
var name = System.IO.Path.GetFileName(pathOrName);
if (string.IsNullOrEmpty(name)) return string.Empty;
int i = name.LastIndexOf('.');
if (i <= 0 || i == name.Length - 1) return string.Empty;
return name.Substring(i).ToLowerInvariant();
} Extra tips: trim input, strip URL query fragments before parsing, and never rely solely on the extension for security — validate file type by MIME/“magic bytes” when accepting uploads. 's manual loop works too, but the LastIndexOf approach above is clearer and easier to test.
Jump to Post— ddanbe 2,724Say L is the last character in your filename.(Same as Fname.Length-1)
Then Fname.Substring(L-3,L); should do the trick.
Jump to Post— rapture 134You 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 …
Say L is the last character in your filename.(Same as Fname.Length-1)
Then Fname.Substring(L-3,L); should do the trick.
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/library/system.io.path.getextension.aspx
(wondering because .aspx is four while .doc is three etc.)
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.
*NEVER* make the assumption that a files extension is only 3 characters.
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;
}
System.IO.FileInfo fi = new System.IO.FileInfo(FileName);
string ext = fi.Extension.ToString();
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.