文件读写操作

 //先判断是否存在文件
            string path = "d:\\localName.txt";
            FileStream fs = new FileStream(path,FileMode.OpenOrCreate);  //没有的话则创建,有则打开
            StreamReader sread = new StreamReader(fs);
            string input = sread.ReadLine();
            if (input != null)
            {
                txtname.Text = input;
                lbtishi.Visible = false;
            }
            sread.Close();
            fs.Close();

 

private void txtname_Leave(object sender, EventArgs e)
        {
            string path = "d:\\localName.txt";
            //创建一个文件流

    FileStream fss = new FileStream(path,FileMode.Open);   //存在文件则对文件进行修改
            if (txtname.Text == null || txtname.Text == "")
            {
                MessageBox.Show("请您输入用户名!");
            }
            else
            {
                //创建写入器
                StreamWriter sw = new StreamWriter(fss);
                sw.WriteLine(txtname.Text);
                sw.Close();
                fss.Close();
                lbtishi.Visible = false;
            }
        }

 

posted @ 2011-09-29 14:59  用心玲听  阅读(131)  评论(0编辑  收藏  举报