日期:2014-05-17 浏览次数:21082 次
using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace fileContainTest
{
public partial class TestForm : Form
{
string filePath = @"D:\VS2008\test\fileContainTest\file.txt";
public TestForm()
{
InitializeComponent();
}
private void btnTest_Click(object sender, EventArgs e)
{
string strFile = readFile(filePath);
if (strFile.Contains("00 00 01 FC"))
MessageBox.Show("含有");
}
private string readFile(string path)
{
StreamReader fileReader = new StreamReader(filePath);
StringBuilder strBuildFile = new StringBuilder();
string fileLine="";
while (fileLine != null)
{
fileLine = fileReader.ReadLine();
strBuildFile.Append(fileLine);
}
return strBuildFile.ToString();
}
}
}
------解决方案--------------------
bool bFound = false;
int pos = -1;
string strFilename = @"E:\Users\matrixchen\Videos\day3part1_2MB_ch9.wmv";
FileStream stream = File.Open(strFilename, FileMode.Open);
byte b1 = 0x00;
byte b2 = 0x00;
byte b3 = 0x01;
byte b4 = 0xFC;
if (stream != null)
{
int baseLen = 102400;
byte[] buff = new byte[baseLen + 3];
int nRead = stream.Read(buff, 0, buff.Length);
int nBasePos = 0;
while (nRead > 3 && !bFound)
{
for (int i = 0; i < nRead - 3; i++)
{
if (buff[i] == b1 && buff[i + 1] == b2 && buff[i + 2] == b3 && buff[i + 3] == b4)
{
bFound = true;
pos = nBasePos + i;
break;
}
}
nBasePos += baseLen;
stream.Seek(-3, SeekOrigin.Current);
nRead = stream.Read(buff, 0, buff.Length);
}
stream.Close();
stream.Dispose();
}