Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi guys,

I made a post not long ago.

Instead of working on my main program i decided to work on a smaller program for testing.

my problem i am having atm, is if i use a file sig i put hard coded in my code.. it works... but if i tell it to use my string value... it doesnt like it... even though the value is exactly the same...

I will post my code, and then explain a little more at the end of the post:

My XML File:

XML
<?xml version="1.0"?>
<Filesigs>
  <Images>
    <sig name = "jpg">FF-0D-FF-E0</sig>
    <sig name = "jpg">FF-D8-FF-E8</sig>
    <sig name = "png">89-50-4E-47-0D-0A-1A-0A</sig>
    <sig name = "gif">47-49-46-38-37-61</sig>
    <sig name = "gif">47-49-46-38-39-61</sig>
    <sig name = "tiff">49-20-49</sig>
    <sig name = "tiff">49-49-2A-00</sig>
    <sig name = "bmp">42-4D</sig>
  </Images>
</Filesigs>


my program code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml.Linq;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            // open file
            FileStream fs = new FileStream(@"C:\Users\Alan\Desktop\Koala.bob", FileMode.Open);
      
            // allocate buffer for whole file
            byte[] buffer = new byte[20];
            fs.Read(buffer, 0, 20);
            string hex = BitConverter.ToString(buffer);
            textBoxStoredExtension.Text = (Path.GetExtension(@"C:\Users\Alan\Desktop\Koala.bob"));
            string storedFileSig = (hex);
            //string storedFileSig = ("FF-0D-FF-E0");
            textBoxStored.Text = hex;
            XDocument myDoc = XDocument.Load("FileSigs.xml");
            IEnumerable<String> result = from FileSigs in myDoc.Elements()
                                         from Images in FileSigs.Elements()
                                         from sigs in Images.Elements()
                                         where (storedFileSig.Contains(sigs.Value))
                                         select sigs.Attribute("name").Value;
            foreach (String r in result)
            {
                textBoxXMLExtension.Text = r;
            }
            //Console.ReadLine();
        }
    }
}


Ok,

as you can see, my program takes the first 20 bytes of the file.

converts to hex and stores in a string called "hex"

it then gets the file extension of the file and displays it in a text box for now. (will be used further later but not important at the moment)

it then goes to the XML file and checks through the XML file until it finds a value that the "hex" contains.

so... when i am testing... i am using a "image.bob" file... when i read it in.. it returns a 20 byte hex calue with -'s between each byte...

so when i run it: i am given FF-D8-FF-E0-00-10-4A-46-49-46-00-01-01-01-00-60-00-60-00-00 for the 20 bytes from the "image.bob" file.

it then should check the XML and see that the first 1 in my XML file matches as the 20 bytes contains "FF-D8-FF-E0".

because it matches. it should then display .jpg (taken from the XML file) and display it in a text box below the text box that displays the ".bob" file extension from the start. (this WONT work when i use String Hex... but as you can see from my code i have commented out //string storedFileSig = ("FF-0D-FF-E0");.... which does work...

then I want it to display the file name "image.bob" into the listBoxImages that I have on the form.


but i cant seem to get it to work :(

can anyone help me here?

many thanks,
Hypeh
Posted

1 solution

You have these two lines:
<sig name = "jpg">FF-0D-FF-E0</sig>    
<sig name = "jpg">FF-D8-FF-E8</sig>

Your file image.bob starts with:
FF-D8-FF-E0
You don't get any result because neither one matches -its a mixture of both.
If you look carefully than your sample is different:
FF-0D-FF-E0
and matches the first line.
So everything behaves like expected.

I assume you need to add FF-D8-FF-E0 to your XML file.
 
Share this answer
 
Comments
HypeH 12-Mar-11 15:52pm    
my friend...

i am an idiot! lol thank you so much.

Ok can i ask another question :)

Now am i able to get it to output file sig in the XML to a text box? or store it in a location. coz i will need to use it later on in my proper program.

many thanks in advance! I love u right now! lol i been staring at it for like 3 hours scratching my head lol!
HypeH 12-Mar-11 17:01pm    
Ok...

I have added Code into my proper project now.

I am getting an error:
XMLHex does not exist in current context
XMLExtension does not exist in current context

maybe you can show me how to fix this?

public string FileSigCheck(string inDir, string inFile)
{
string strFileType = "";

// opens the current file
FileStream fs = new FileStream(inDir + "\\" + inFile, FileMode.Open);

// allocate buffer for whole file
byte[] buffer = new byte[20];

fs.Read(buffer, 0, 20);

string hex = BitConverter.ToString(buffer);

string storedFileSig = hex;

XDocument myDoc = XDocument.Load("FileSigs.xml");

IEnumerable<string> ImagesResult1 = from FileSigs in myDoc.Elements()
from Images in FileSigs.Elements()
from sigs in Images.Elements()
where (storedFileSig.Contains(sigs.Value))
select sigs.Attribute("name").Value;

IEnumerable<string> ImagesResult2 = from FileSigs in myDoc.Elements()
from Images in FileSigs.Elements()
from sigs in Images.Elements()
where (storedFileSig.Contains(sigs.Value))
select sigs.Value;

foreach (String r in ImagesResult1)
{
string XMLExtension = r;
}

foreach (String r in ImagesResult2)
{
string XMLHex = r;
}

if (hex.Contains(XMLHex) && (!inFile.Contains(XMLExtension))) //use sub string
{
strFileType = "Suspect";
}
else if (hex == XMLHex)
{
strFileType = "Image";
}
else if (hex != XMLHex)
{
strFileType = "Not Valid JPG File";
}
return strFileType;
}
Robert Rohde 12-Mar-11 17:23pm    
It doesn't work because you declare XMLExtension and XMLHex within the loop. Thus its only visible there. Declare it outside and it'll work.
You should also optimizeyour two linq queries into one:
var result = (from FileSigs in myDoc.Elements()
from Images in FileSigs.Elements()
from sigs in Images.Elements()
where (storedFileSig.Contains(sigs.Value))
select new { Name = sigs.Attribute("name").Value, Value = sigs.Value }).
FirstOrDefault();

string xmlExtension = result == null ? "" : result.Name;
string xmlHex = result == null ? "" : result.Value;

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