Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
can i get extension from file name
my file is
 
D:\Sam\DSC0001
 
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
good
Please Sign up or sign in to vote.

Solution 1

Have you tried this : system.io.path.getextension[^]
  Permalink  
Comments
hareshPrj at 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
good
Please Sign up or sign in to vote.

Solution 2

Try:
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)
            {
            Console.WriteLine(Path.GetExtension(f));
            }
        }
    }
  Permalink  
Comments
hareshPrj at 24-Dec-12 2:57am
   
no sir not working
OriginalGriff at 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 at 24-Dec-12 3:09am
   
sir actually i have path with file name like this
 
D:\sam\DSC0001
 
and i want to file extension of DSC0001 file.
OriginalGriff at 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 at 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 at 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 at 24-Dec-12 4:07am
   
sir their is no range of files..... :-)
OriginalGriff at 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
good
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
 

  Permalink  

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

  Print Answers RSS
0 Afzaal Ahmad Zeeshan 256
1 OriginalGriff 251
2 BillWoodruff 240
3 Maciej Los 230
4 Sergey Alexandrovich Kryukov 195
0 OriginalGriff 6,419
1 Sergey Alexandrovich Kryukov 6,008
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,070


Advertise | Privacy | Mobile
Web04 | 2.8.1411019.1 | Last Updated 26 Dec 2012
Copyright © CodeProject, 1999-2014
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