VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • C#教程之C#文件操作的简单实例

文件的读取
复制代码 代码如下:

 FileStream fs = new FileStream(@"D:\12.txt", FileMode.Open);
            byte[] buffer = new byte[1024 * 1024];
            fs.Read(buffer, 0, buffer.Length);
            string content = Encoding.Default.GetString(buffer);
            textBox1.Text = content;
            fs.Dispose();

文件的保存
复制代码 代码如下:

 SaveFileDialog sfd = new SaveFileDialog();
            DialogResult rst = sfd.ShowDialog();
            if(rst==System.Windows.Forms.DialogResult.OK)
            {
                FileStream fs = new FileStream(sfd.FileName,FileMode.Create);
                string content = textBox1.Text;
                byte[] buffer = ASCIIEncoding.UTF8.GetBytes(content);
                fs.Write(buffer,0,buffer.Length);
                fs.Dispose();

文件的复制
复制代码 代码如下:

FileStream streamread = new FileStream(@"D:\123.wmv",FileMode.Open);
            FileStream streamwrite = new FileStream(@"F:\1212.wmv",FileMode.Create);
            byte[]buffer=new byte[1024*1024*3];
            int Length;
            do
            {
                Length = streamread.Read(buffer,0, buffer.Length);
                streamwrite.Write(buffer,0, Length);
            }
            while (Length == buffer.Length);
            streamread.Dispose();
            streamwrite.Dispose();
            MessageBox.Show("Copy Success");

相关教程