最近碰上这么一个需求,用C# winform做个东西,点按钮A后从1.txt里读取文本,而且是一行一行的读,每读完一行就响应事件B,用C#应该怎么写好点。

解决方案 »

  1.   

    msdn里面有详细的例子,跟你的要求正好一模一样。
      

  2.   

    可是我这机子上没装有MSDN郁闷了
      

  3.   

    事件的响应是需要别人触发的,你是不是说触发事件B?
    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();
        }
    }
      

  4.   


    StreamReader sr = new StreamReader("需读取的文件");
    string str;while ((str = sr.ReadLine()) != null)
    {
        做点啥(str);
    }
      

  5.   

    正好前几天在做public ArrayList txtArrList(string FilePath, string FileName)
            { 
                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))
                    {
                        //这里写读出每一行数据后你进行的操作
                    }
                }
            }
      

  6.   

    这个事件B是MSN公开的发送聊天文字的事件,不是自己写的哦,如题:
                try
                {
                    ActiveConversation.SendTextMessage(message);//调用发送事件                //PrintText(_messenger.ContactList.Owner, message);//输入文的打印            }
    我现在做的是一个带翻译功能的东西,简单的说就是和MSN的Tbot聊天,然后把记录导出来,现在发现Tbot的文字回复方式是每一句话都lock起来了,不知道能不能实现。
      

  7.   

    StreamReader reader = File.OpenText("文件");
    A.click()
    while(EndOfStream)
    {
      reader.readline();
      /////////////////////
      //在这里添加你要的b事件
      /////////////////////
    }
    reader .close();
      

  8.   

    我以前写过一个例子 或许有用:
    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();
                }
            }
        }
    }
      

  9.   

    这是主函数:
    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();
            } 
        }
    }
      

  10.   

    哇,现在又碰上一个郁闷的问题,需求是这样,从1.TXT里读出一行就发响应一次事件B,这回又怎么搞