简单的文件读写操作

终于有空下来了,几乎是把前段时间看得异步回调又给忘了,但还好,想得开,嘿嘿

学习本来就是一个离散再结合的过程

回忆一下,不怎么花功失就复习了一遍,贴个异步读取文件的DEMO
  class Program
    {
        class FileStatus
        {
            public byte[] Buffer;
            public FileStream Fs;
            public int ID;
        }

        private static readonly int NBytesPerFile = 10000000;
        private static readonly int NRead = 5;
        private static string FileName = "blob.bin";

        static void CreateBlobFile()
        {
            byte[] blob = new byte[NBytesPerFile];
            FileStream fs =
                new FileStream(FileName, FileMode.Create, FileAccess.Write, FileShare.None, 4096, false);
            fs.Flush();
            fs.Close();
                   
        }

        static void Main(string[] args)
        {
            CreateBlobFile();
            //创建好文件,开始复制
            for(int i=0;i<NRead;i++)
            {
                FileStream fs = new FileStream(FileName,
                    FileMode.Open, FileAccess.Read,FileShare .Read, 4096, true);

                FileStatus status = new FileStatus();
                status.ID = i;
                status.Fs = fs;
                status.Buffer = new byte[NBytesPerFile];

                Console.WriteLine("Thread #{0}:Trigger asynchronous read #{1}",
                                  System.Threading.Thread.CurrentThread.ManagedThreadId, i);
                //开始异步读取
                fs.BeginRead(status.Buffer,
                    0, NBytesPerFile, new AsyncCallback(callback), status);

            }
            Console.ReadLine();
        }
        //该方法会在读取完毕时调用,而其线程是与发出回调的线程是同一个的
        static void callback(IAsyncResult asyncResult)
        {
            FileStatus status = asyncResult.AsyncState as FileStatus;
            if(status!=null)
            {
                byte[] data = status.Buffer;
                FileStream fs = status.Fs;
               
                Console.WriteLine("Thread #{0}:Begin work on data #{1}"
                                  , System.Threading.Thread.CurrentThread.ManagedThreadId
                                  , status.ID);
 //在这里等待,读取完 即等待挂起的异步读取完成
                int nbytesRead = fs.EndRead(asyncResult);

                //在close这个地方把缓冲的读进来才关闭
                fs.Close();
                System.Threading.Thread.Sleep(1000);
                Console.WriteLine("Thread #{0}:End work on data #{1}",
                                  System.Threading.Thread.CurrentThread.ManagedThreadId,
                                  status.ID);
                
            }
        }
    }

运行的结果图


说到文件操作,无非就是读写,昨晚看一篇讲病毒史的文章,提到一种自复制自运行的
自已也就写写,运行就算了,呆会不知会咋样,自复制总行吧,
说白了也就是复制二进制的EXE文件喽
 string sExe = System.AppDomain.CurrentDomain.FriendlyName;
            string s = Application.ExecutablePath;
            
            FileStream inStream = File.OpenRead(s);
            Random ra = new Random();
            FileStream OutStream = File.OpenWrite(ra.Next().ToString()+ "copy.exe");

            byte[] buffer = new System.Byte[bufferSize];
            int nBytesRead = 0;
            while ((nBytesRead = inStream.Read(buffer, 0, bufferSize)) > 0)
            {
                OutStream.Write(buffer, 0, nBytesRead);

            }
//记得随手关门
            inStream.Close();
            OutStream.Close();

其实有时觉得挺无聊的!感觉那些设计模式好吸引人呀!
可由于我的工作是WINFORM,更多是跟WINDOWS打交道,我需要知道WIN32 API等等WINDOWS,C++类的编程

有时感到迷茫,不知是否该再坚持下去,看到那些什么ASP.NET,CSS JS就一头雾水
但看到一个新来的一个同事用Hibernate很好,也认识好几个模式,说得很清楚,连一个C++函数,一个结构体概念都很模糊时,
我就更加不知道,C#在做应用软件的路到底有多长!

posted @ 2008-07-04 16:06  yellowyu  阅读(311)  评论(0编辑  收藏  举报