Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.50/5 (4 votes)
See more:
If a file let's say 'New Doc.docx' is renamed to 'New Doc.txt'

How can I determine that the file was of extension docx
Posted
Updated 5-Apr-11 5:46am
v2
Comments
Sandeep Mewara 5-Apr-11 11:15am    
Elaborate more on what are you trying to do?
Toli Cuturicu 5-Apr-11 13:10pm    
Absurdity. You can't (of course).
Toli Cuturicu 5-Apr-11 13:11pm    
And... Never post fake answers! I wasn't nice and deleted yours.
Henry Minute was nicer and copied it as a comment.

You can only determine what the extension currently is, not what it used to be.
 
Share this answer
 
Comments
Henry Minute 5-Apr-11 12:11pm    
From the OP

means no technique to do so?

actually i have developed an application that opens only Word 2003 files. but if any one makes file on Word 2007 and rename its extension from docx to doc. my application gets stuck some time.
Sergey Alexandrovich Kryukov 5-Apr-11 12:14pm    
Oh gosh!
--SA
Sergey Alexandrovich Kryukov 5-Apr-11 12:14pm    
He he... my 5.
--SA
Look at the file stream before you try to use it. .docx files are just renamed .zip files, so I think if the first two bytes of the file stream are PK then you can be fairly certain that someone renamed a .docx file and you can avoid opening it.
 
Share this answer
 
it's funny, your problem is in the format of the file, if you're only opening Word 2003 files then your app doesn't know how to handle the 2007 files and renaming it won't change that fact.

I'll show you what I mean, change the extension of the docx file to zip and open it using any application that can read zipped files... now try the same with a doc file... see the problem?

and btw, there's no way to know if the extension of a file was changed previously
 
Share this answer
 
v2
some body gave me this link in above discussion
http://www.garykessler.net/library/file_sigs.html

it has solved my problem..
thanks bro.


C#
//http://www.garykessler.net/library/file_sigs.html

            int i = 0;
            int count = 0;
            OpenFileDialog op = new OpenFileDialog();
            if (op.ShowDialog() == DialogResult.OK)
            {
                FileStream fstream = new FileStream(op.FileName, FileMode.Open);
                while ((i = fstream.ReadByte()) != -1)
                {
                    if (count == 10) break;
                    count++;
                    textBox1.Text += i.ToString() + " ";
                }

                //80 75 3 4 20 0 6 0 8 0 is bytecode for docx
                if (textBox1.Text.Equals("80 75 3 4 20 0 6 0 8 0 "))
                {
                    MessageBox.Show("docx file");
                    return;
                }
                MessageBox.Show("Not Docx");
            }
 
Share this answer
 
v3
Comments
JOAT-MON 6-Apr-11 0:50am    
Cool, glad you got a solution!

[EDIT] Wrapped code in code block.
hotthoughtguy 6-Apr-11 1:21am    
ok
Trial and error method: rename it (with the supposed original extension) and try to open it with an application requiring files with such an extension.
:-)
 
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