Click here to Skip to main content
14,326,463 members
Rate this:
Please Sign up or sign in to vote.
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.
Rate this:
Please Sign up or sign in to vote.

Solution 1

You can only determine what the extension currently is, not what it used to be.
   
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.
   
Oh gosh!
--SA
   
He he... my 5.
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 7

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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 4

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
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 6

some body gave me this link in above discussion
http://www.garykessler.net/library/file_sigs.html

it has solved my problem..
thanks bro.


//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");
            }
   
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
Rate this:
Please Sign up or sign in to vote.

Solution 5

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.
:-)
   

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




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