判断一文件是word文档,用c#或vb.net语言 http://topic.csdn.net/u/20071116/22/6af67454-b6f3-4ba1-95f2-6ce6c527cb74.html C# code using System.Runtime.InteropServices; [DllImport("ntdll.dll")] public static extern int RtlCompareMemory(IntPtr Destination, IntPtr Source, int Length); // 比较两个内存块是否相同 private void button1_Click(object sender, EventArgs e) { byte[] vHead = new byte[] { 0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1 }; // Doc文件的头标志 FileStream vFileStream = new FileStream(@"c:\temp\temp.doc", FileMode.Open, FileAccess.Read); byte[] vBuffer = new byte[vHead.Length]; vFileStream.Read(vBuffer, 0, vBuffer.Length); // 读取文件的头信息 vFileStream.Close(); if (RtlCompareMemory(Marshal.UnsafeAddrOfPinnedArrayElement(vHead, 0), Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0), vHead.Length) == vHead.Length) { MessageBox.Show("估计是Word文档"); } }
评论