private void btuDu_Click(object sender, EventArgs e)
        {
             //声明变量
            string path;
              //接收要读取的文件名路径
            path = txtNames.Text;            try
            {
                //判断文件是否存在
                if (!File.Exists(path))
                {
                    MessageBox.Show("文件不存在");
                }
                else
                {
                    //打开流以进行读取
                    FileStream fa = File.OpenRead(path);
                       //把读取到的数据存放在arr []中,并在txtName.Text文本框显示
                      byte[] arr = new UTF8Encoding(true).GetBytes(txtName.Text);
                     //byte[] arr = new byte[1000];
                   // UTF8Encoding data = new UTF8Encoding(true);
                      //一直到读完
                    while(fa.Read(arr,0,arr.Length)>0)
                    {
                        MessageBox.Show("dddd");
                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("出现错误"+ex.Message);
            }
        }
    }我这样读取文件,即不报错又不成功,
错在那里呢????

解决方案 »

  1.   

                                                byte[]   arr   =   new   UTF8Encoding(true).GetBytes(txtName.Text);这问题还不大?设个断点跟进去看看吧,你写的是什么意思呀。
      

  2.   

    byte[] arr =  new UTF8Encoding(true).GetBytes(txtName.Text); 
    这句是将读取的内容在 txtName.Text文本框显示
      

  3.   

    byte[]   arr   =     new   UTF8Encoding(true).GetBytes(txtName.Text);  
    这句是将读取的内容在   txtName.Text文本框显示
    ----------------------------------
    txtName.Text是你要赋值的对象,
    Encoding.GetBytes(string)string是个传入的进行字节编码的参数。
    逻辑错误。
    ----
    using System;
    using System.IO;class Test 
    {
        public static void Main() 
        {
            try 
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader("TestFile.txt")) 
                {
                    String line;
                    // Read and display lines from the file until the end of 
                    // the file is reached.
                    while ((line = sr.ReadLine()) != null) 
                    {
                        Console.WriteLine(line);
                    }
                }
            }
            catch (Exception e) 
            {
                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
        }
    }
      

  4.   

    LZ在四楼写的东西真是令人惊讶。我十分怀疑MS的支持人员看到这个作何感想。撞墙?
      

  5.   

    哈哈哈,谢谢大家了
    在大家的支持和帮助下
    我终于成功了功能是:读写文件
    从一个文件读取数据,写入另一个文件中using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;namespace Hangjiazuoye
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void btuQueding_Click(object sender, EventArgs e)
            {
                //接收要读取的文件名路径 
                string path = textName.Text;
                 //存放文件路径 
                string paths = textAddress.Text;            try
                {
                    //检查文件是否存在
                    if (!File.Exists(path))
                    {
                        MessageBox.Show("文件不存在");
                    }
                    else {
                        //打开流读取文件
                        FileStream fs = File.OpenRead(path);
                       
                        fs.Flush();
                        //创建一个数组,存放数据
                        byte[] arr=new byte[10000];
                        //用Create指定名称新建一个文件
                        FileStream ff = File.Create(paths);                    //循环读取数据
                        while(fs.Read(arr,0,arr.Length)>0)
                        {
                            //写数据
                            ff.Write(arr,0,arr.Length);                        MessageBox.Show("文件已经读取");
                             //关闭流
                            ff.Close();
                        }
                    }
                }
              
                catch(Exception ex)
                {
                    MessageBox.Show("有错误"+ex.Message);
                }
               
            }
        }
    }