我以前写过一个例子 或许有用: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.IO; namespace Text { //定义了一个读写委托。 delegate void Read(CopyObject obj); delegate void Write(CopyObject obj); public class CopyEvents { private event Read ReadEvent; private event Write WriteEvent; //定义一个复制的对象 public CopyObject copyObjects; public CopyObject CopyObjects { get { return copyObjects; } set { copyObjects = value; } } //定义一个Run方法 public void Run() { ReadEvent += new Read(CopyEvents_ReadEvent); WriteEvent += new Write(CopyEvents_WriteEvent); Thread Th = new Thread(new ThreadStart(Execute_Copy)); Th.Start(); } //定义一个执行复制的方法 public void Execute_Copy() { ReadEvent(CopyObjects); } //定义一个执行复制写的事件 void CopyEvents_WriteEvent(CopyObject obj) { using (FileStream fs = new FileStream(obj.Target_FileName, FileMode.Append, FileAccess.Write, FileShare.Write)) { fs.Write(obj.File_bytes,0,obj.File_bytes.Length); fs.Close(); } } //定义一个执行复制读的事件 void CopyEvents_ReadEvent(CopyObject obj) { using (FileStream fs = new FileStream(obj.Filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { long len = fs.Length; //定义开始的长度为0字节 long wLen = 0; //定义一次性读取1048576个字节 long bytLen = 1048576000; while (wLen < len) { long pos = fs.Position; wLen += bytLen; if (wLen > len) { bytLen = len - pos; wLen = len; } byte[] byt = new byte[bytLen]; fs.Read(byt, 0, byt.Length); fs.Seek(wLen, SeekOrigin.Begin); obj.File_bytes = byt; WriteEvent(obj); } fs.Close(); } } } public class CopyObject { //定义开始的文件字节大小 private byte[] file_bytes; public byte[] File_bytes { get { return file_bytes; } set { file_bytes = value; } } private string filename; public string Filename { get { return filename; } set { filename = value; } } private string target_FileName; public string Target_FileName { get { return target_FileName; } set { target_FileName = value; } } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Threading; namespace Text { public class CopyThread { private CopyObject copyObjects; public CopyObject CopyObjects { get { return copyObjects; } set { copyObjects = value; } } public void Run() { Thread ThRead = new Thread(new ParameterizedThreadStart(CopyEvents_ReadEvent)); ThRead.Start(CopyObjects); } //复制写的方法 void CopyEvents_WriteEvent(object COB) { CopyObject obj = (CopyObject)COB; using (FileStream fs = new FileStream(obj.Target_FileName, FileMode.Append, FileAccess.Write, FileShare.Write)) { fs.Write(obj.File_bytes, 0, obj.File_bytes.Length); fs.Close(); } } //复制读的方法 void CopyEvents_ReadEvent(object COB) { CopyObject obj = (CopyObject)COB; using (FileStream fs = new FileStream(obj.Filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { long len = fs.Length; //定义开始的长度为0字节 long wLen = 0; //定义一次性读取1048576个字节 long bytLen = 1048576000; while (wLen < len) { //Position规定他是一个从0开始的索引 long pos = fs.Position; wLen += bytLen; if (wLen > len) { bytLen = len - pos; wLen = len; } byte[] byt = new byte[bytLen]; fs.Read(byt, 0, byt.Length); fs.Seek(wLen, SeekOrigin.Begin); obj.File_bytes = byt; Thread ThWrite = new Thread(new ParameterizedThreadStart(CopyEvents_WriteEvent)); ThWrite.Start(obj); } fs.Close(); } } } }
这是主函数: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.IO; namespace Text { public class test { public static void Main() { CopyObject CO = new CopyObject(); CO.Filename = "A.txt"; CO.Target_FileName = "B.txt"; test t = new test(); t.Copy(CO); } private void Copy(CopyObject Obj) { //用事件 //CopyEvents CE = new CopyEvents(); //CE.CopyObjects = Obj; //CE.Run(); //用线程 CopyThread CT = new CopyThread(); CT.CopyObjects = Obj; CT.Run(); } } }
class Program
{
public delegate void BDelegate(string line);
public static event BDelegate B;
static void Main(string[] args)
{
StreamReader reader = File.OpenText("文件");
string line = reader.ReadLine();
while (!string.IsNullOrEmpty(line))
{
if (B != null) B(line);
line = reader.ReadLine();
}
reader.Close();
TestRegex12();
Console.ReadKey();
}
}
StreamReader sr = new StreamReader("需读取的文件");
string str;while ((str = sr.ReadLine()) != null)
{
做点啥(str);
}
{
ArrayList txtList=new ArrayList();
StreamReader txtReader = new StreamReader(FilePath + FileName);
string txtline = ""; while (txtline != null)
{
txtline = txtReader.ReadLine();
if (txtline != null)
{
txtList.Add(txtline);
}
}
txtReader.Close(); return txtList;
} private void button1_Click(object sender, EventArgs e)
{
string FilePath = "g:\\";//路径
string FileName = "WO.txt";//文件名 ArrayList txtList = txtArrList(FilePath, FileName);
int count = 0;
foreach (string line in txtList)
{
if (!String.IsNullOrEmpty(line))
{
//这里写读出每一行数据后你进行的操作
}
}
}
try
{
ActiveConversation.SendTextMessage(message);//调用发送事件 //PrintText(_messenger.ContactList.Owner, message);//输入文的打印 }
我现在做的是一个带翻译功能的东西,简单的说就是和MSN的Tbot聊天,然后把记录导出来,现在发现Tbot的文字回复方式是每一句话都lock起来了,不知道能不能实现。
A.click()
while(EndOfStream)
{
reader.readline();
/////////////////////
//在这里添加你要的b事件
/////////////////////
}
reader .close();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
namespace Text
{
//定义了一个读写委托。
delegate void Read(CopyObject obj);
delegate void Write(CopyObject obj);
public class CopyEvents
{
private event Read ReadEvent;
private event Write WriteEvent;
//定义一个复制的对象
public CopyObject copyObjects;
public CopyObject CopyObjects
{
get
{
return copyObjects;
}
set
{
copyObjects = value;
}
} //定义一个Run方法
public void Run()
{
ReadEvent += new Read(CopyEvents_ReadEvent);
WriteEvent += new Write(CopyEvents_WriteEvent);
Thread Th = new Thread(new ThreadStart(Execute_Copy));
Th.Start();
} //定义一个执行复制的方法
public void Execute_Copy()
{
ReadEvent(CopyObjects);
} //定义一个执行复制写的事件
void CopyEvents_WriteEvent(CopyObject obj)
{
using (FileStream fs = new FileStream(obj.Target_FileName, FileMode.Append, FileAccess.Write, FileShare.Write))
{
fs.Write(obj.File_bytes,0,obj.File_bytes.Length);
fs.Close();
}
} //定义一个执行复制读的事件
void CopyEvents_ReadEvent(CopyObject obj)
{
using (FileStream fs = new FileStream(obj.Filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
long len = fs.Length;
//定义开始的长度为0字节
long wLen = 0;
//定义一次性读取1048576个字节
long bytLen = 1048576000;
while (wLen < len)
{
long pos = fs.Position;
wLen += bytLen;
if (wLen > len)
{
bytLen = len - pos;
wLen = len;
}
byte[] byt = new byte[bytLen];
fs.Read(byt, 0, byt.Length);
fs.Seek(wLen, SeekOrigin.Begin);
obj.File_bytes = byt;
WriteEvent(obj);
}
fs.Close();
}
}
} public class CopyObject
{
//定义开始的文件字节大小
private byte[] file_bytes; public byte[] File_bytes
{
get
{
return file_bytes;
}
set
{
file_bytes = value;
}
}
private string filename; public string Filename
{
get
{
return filename;
}
set
{
filename = value;
}
}
private string target_FileName; public string Target_FileName
{
get
{
return target_FileName;
}
set
{
target_FileName = value;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
namespace Text
{
public class CopyThread
{
private CopyObject copyObjects;
public CopyObject CopyObjects
{
get
{
return copyObjects;
}
set
{
copyObjects = value;
}
} public void Run()
{
Thread ThRead = new Thread(new ParameterizedThreadStart(CopyEvents_ReadEvent));
ThRead.Start(CopyObjects);
} //复制写的方法
void CopyEvents_WriteEvent(object COB)
{
CopyObject obj = (CopyObject)COB;
using (FileStream fs = new FileStream(obj.Target_FileName, FileMode.Append, FileAccess.Write, FileShare.Write))
{
fs.Write(obj.File_bytes, 0, obj.File_bytes.Length);
fs.Close();
}
} //复制读的方法
void CopyEvents_ReadEvent(object COB)
{
CopyObject obj = (CopyObject)COB;
using (FileStream fs = new FileStream(obj.Filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
long len = fs.Length;
//定义开始的长度为0字节
long wLen = 0;
//定义一次性读取1048576个字节
long bytLen = 1048576000;
while (wLen < len)
{
//Position规定他是一个从0开始的索引
long pos = fs.Position;
wLen += bytLen;
if (wLen > len)
{
bytLen = len - pos;
wLen = len;
}
byte[] byt = new byte[bytLen];
fs.Read(byt, 0, byt.Length);
fs.Seek(wLen, SeekOrigin.Begin);
obj.File_bytes = byt; Thread ThWrite = new Thread(new ParameterizedThreadStart(CopyEvents_WriteEvent));
ThWrite.Start(obj);
}
fs.Close();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
namespace Text
{
public class test
{
public static void Main()
{
CopyObject CO = new CopyObject();
CO.Filename = "A.txt";
CO.Target_FileName = "B.txt";
test t = new test();
t.Copy(CO);
}
private void Copy(CopyObject Obj)
{
//用事件
//CopyEvents CE = new CopyEvents();
//CE.CopyObjects = Obj;
//CE.Run(); //用线程
CopyThread CT = new CopyThread();
CT.CopyObjects = Obj;
CT.Run();
}
}
}