Click here to Skip to main content
15,940,550 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


Have you tried this :[^]
Share this answer
hareshdgr8 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
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)
Share this answer
hareshdgr8 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:
hareshdgr8 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).
hareshdgr8 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 ....
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
Share this answer

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900