Click here to Skip to main content
11,928,919 members (54,024 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB VB.NET
can i get extension from file name
my file is


now i want to check DSC0001 extension and file exist or not

please help me

Smile | :)
Posted 23-Dec-12 21:31pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Have you tried this :[^]
hareshPrj 24-Dec-12 2:47am
yes sir i tried but it can't work it give me blank extension because i have file name only like DSC0001 not a DSC001.txt
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

string path = @"D:\Sam\DSC0001";
int index = path.LastIndexOf('\\');
if (index > 0)
    string dir = path.Substring(0, index);
    string file = path.Substring(index + 1);
    string[] files = Directory.GetFiles(dir, file + ".*");
    if (files.Length > 0)
        // File exists.
        foreach (string f in files)
hareshPrj 24-Dec-12 2:57am
no sir not working
OriginalGriff 24-Dec-12 3:04am
If you don't have an extension, then it will return a blank.
You cannot get an extension where none exists! :laugh:
hareshPrj 24-Dec-12 3:09am
sir actually i have path with file name like this


and i want to file extension of DSC0001 file.
OriginalGriff 24-Dec-12 3:32am
If it has an extension, then the above code will show it - so we have to look at what you are trying to actually do.
I suspect that you have a file called "DSC0001" that does not have an extension, and you are trying to work out what type of file it is, and what program can open it.
Let's check:
Open a Windows Explorer window and browse to the folder the file is in. Right click the file, and select "Properties". In the resulting dialog, You fill have a section "Type of file" and "Description".
If the "Type of file" is "file" and the "Description" is "DSC0001" then there is no extension, and you need to work out the content for yourself. If it isn't, then the file has an extension, which will be shown in brackets at the end of the file type. E.g.: "(.jpg)"

If you now know the extension - you got the code wrong.
If there is no extension, then you will need to look inside teh file to work out what file type it is (and probably rename it to give it an extension when you do know).
hareshPrj 24-Dec-12 3:37am
sir actully file name is in database but without extension and i want to load a file with extension now what can i do ....
OriginalGriff 24-Dec-12 4:04am
If the database does not contain the extension, then your only option is to examine the file content and try to work out what type it is. (One of the reasons I try very hard not to throw away information! :laugh:)
Do you have a range of possible file types, or could it be anything at all?
hareshPrj 24-Dec-12 4:07am
sir their is no range of files..... :-)
OriginalGriff 24-Dec-12 4:28am
Then you are royally screwed.
There is nothing that guarantees the content is a particular type: many file types do have a "leading identifier", such as JPG files which always start with FF D8 FF E0 00 01 then the text JFIF, but there is no guarantee that a straight binary file cannot start with this as well. Similarly, an FLV file starts it's data with the letters "FLV", but a text file could easily start with that as well.

If you cannot identify the file extension, then you will probably need to either look at the manually and try to work it out, then add that info to the database, if there are only a couple, or scrap them, and get them all reloaded - saving the original file name and extension when you do.

Sorry - but there is nothing simple you can do to retrieve the information once you have discarded it.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Dim path_ as string = "D:\DSC0001"
Dim fileExtension As String = Path.GetExtension(path_ )
If Not File.Exists(file_path & "." & fileExtension) Then
    msgbox("File Not Found")
End If

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 26 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100