日期:2014-05-19  浏览次数:20780 次

关于C#下文件读取发送的问题
现在有这么一个问题,一个.txt文件,里面只包含任意长度的01字符,现在希望能够对这个文件的内容进行读取,每次读取8位,把其放到独立存储区isoStore中,而每次读取的时间间隔是固定,并可调的,时间间隔最好准确定长。希望各位能够贴上一段代码,帮我解决这个问题,谢谢!!

------解决方案--------------------
public partial class Form1 : Form
{
public FileStream filestream = null;
public BinaryReader streamreader = null;
public string strfilename = "我的文件 ";
/// <summary>
/// 用来存放数据
/// </summary>
public List <byte> isoStore = new List <byte> ();
/// <summary>
/// 计时器
/// </summary>
Timer timer = new Timer();
/// <summary>
/// 用来设置时间间隔
/// </summary>
int ti = 100;

public Form1()
{
InitializeComponent();
}
/// <summary>
/// 读取内容
/// </summary>
/// <returns> </returns>
public bool ReadFile()
{
if (filestream == null)
{
try
{
filestream = new FileStream(strfilename, FileMode.Open);
streamreader = new BinaryReader(filestream);
}
catch (IOException e)
{
}
}
else
{
if (filestream.Position < filestream.Length)
{
byte bb = streamreader.ReadByte();
isoStore.Add(bb);
}
else
{
return false;
}
}
return true;

}

private void Form1_Load(object sender, EventArgs e)
{
timer.Interval = ti;
timer.Enabled = true;
timer.Tick += new EventHandler(timer_Tick);
}

void timer_Tick(object sender, EventArgs e)
{
//读取数据
ReadFile();
}

}