using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO;using System.Linq;using System.Text;using System.Windows.Forms;namespace FileDetect{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string path = System.Environment.CurrentDirectory; DirectoryInfo directory = new DirectoryInfo(path); FileInfo[] fileInfoArray = directory.GetFiles(); StringBuilder sb = new StringBuilder (); foreach (var item in fileInfoArray) { String str = String.Format("{0}: {1}",item.Name,IsPE32(item.FullName)? "32bit":"64bit"); sb.AppendLine(str); } textBox1.Text = sb.ToString(); } public static bool IsPE32(string path) { FileStream stream = File.OpenRead(path); //移动到e_lfanew的位置处 stream.Seek(0x40 - 4, SeekOrigin.Begin); byte[] buf = new byte[4]; stream.Read(buf, 0, buf.Length); //根据e_lfanew的值计算出Machine的位置 int pos = BitConverter.ToInt32(buf, 0) 4; stream.Seek(pos, SeekOrigin.Begin); buf = new byte[2]; stream.Read(buf, 0, buf.Length); //得到Machine的值,0x14C为32位,0x8664为64位 Int16 machine = BitConverter.ToInt16(buf, 0); if (machine == 0x14C) { return true; } else { return false; } } }}
评论