Slant-shadow Posted March 7, 2014 Report Share Posted March 7, 2014 Можете подсказать какое условие нужно указать, чтобы возвращалось нужное значение, а не первое попавшееся. В конкретном примере нужно значение JPG, а возвращается первое PSD using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace ConsoleApplication9 { class Program { static void Main(string[] args) { byte[] content = File.ReadAllBytes(@"D:\error.jpg"); Data Newdata = new Data(); Newdata.Format(content); Console.ReadLine(); } } public class Data { public byte[] bytes; public string name; List<Data> formats = new List<Data>(); public string Format(byte[] content) { formats.Add(new Data() { name = "PNG", bytes = new byte[] { 137, 80, 78, 71, 13, 10, 26, 10 } }); formats.Add(new Data() { name = "ACCDB", bytes = new byte[] { 0, 1, 0, 0, 83, 116, 97, 110, 100, 97, 114, 100, 32, 65, 67, 69, 32, 68, 66 } }); formats.Add(new Data() { name = "MDB", bytes = new byte[] { 0, 1, 0, 0, 83, 116, 97, 110, 100, 97, 114, 100, 32, 74, 101, 116, 32, 68, 66 } }); formats.Add(new Data() { name = "IMG", bytes = new byte[] { 0, 1, 0, 8, 0, 1, 0, 1, 1 } }); formats.Add(new Data() { name = "MDF", bytes = new byte[] { 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 2, 0, 1 } }); formats.Add(new Data() { name = "PIC", bytes = new byte[] { 1, 0, 0, 0, 1 } }); formats.Add(new Data() { name = "MDF", bytes = new byte[] { 1, 15, 0, 0 } }); formats.Add(new Data() { name = "XLS", bytes = new byte[] { 9, 8, 16, 0, 0, 6, 5, 0 } }); formats.Add(new Data() { name = "DOC", bytes = new byte[] { 13, 68, 79, 67 } }); formats.Add(new Data() { name = "PPT", bytes = new byte[] { 15, 0, 232, 8 } }); formats.Add(new Data() { name = "PDF", bytes = new byte[] { 37, 80, 68, 70 } }); formats.Add(new Data() { name = "7Z", bytes = new byte[] { 55, 122, 188, 175, 39, 28 } }); formats.Add(new Data() { name = "PSD", bytes = new byte[] { 56, 66, 80, 83 } }); formats.Add(new Data() { name = "JPG", bytes = new byte[] { 255, 216, 255, 224 } }); return formats.Where(x => content.SequenceEqual(x.bytes)) .Select(x => x.name) .FirstOrDefault(); string extension = ""; foreach (Data format in formats) { for (int i = 0; i < format.bytes.Length; i++) { if (format.bytes == content) { continue; } else if (format.bytes != content) { Console.WriteLine("Сравниваемые байты не равны"); } } extension = format.name; Console.WriteLine(extension); } return extension; } } } Link to comment Share on other sites More sharing options...
Teddy_Bear Posted March 7, 2014 Report Share Posted March 7, 2014 У Вас, видимо, много раз выдает "Сравниваемые байты не равны", после чего format.name остается неопределённым. В частности psd. После проведенной проверки по выходу их Foreach нигде нет определённости совпадения заголовочной части. Может быть, вместо побайтного перебора вырезать кусок нужной длины и сравнивать один раз для каждого типа файла и в случае успеха приравнивать тип (имя). Link to comment Share on other sites More sharing options...
Slant-shadow Posted March 13, 2014 Author Report Share Posted March 13, 2014 Спасибо Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now