C#打開一個文本文件並讀寫


OpenFileDialog OFD = new OpenFileDialog();
            OFD.Title = "打開第一個文本文件";
            OFD.FileName = "*.txt";
            OFD.InitialDirectory = WorkSpacePath;
            if (OFD.ShowDialog() == DialogResult.OK)
            {
                FileInfo fi = new FileInfo(OFD.FileName);
                StreamReader sr = fi.OpenText();
                while (sr.Peek() > 0)
                {
                    listBox1.Items.Add(sr.ReadLine());
                }
                sr.Close();
                textBox1.Text = OFD.FileName;
                this.statusStrip1.Items[1].Text = listBox1.Items.Count.ToString();
            }

寫一個文本文件:

string Path = Application.StartupPath;
            FileStream fs = new FileStream(Path + "\\path.dll", FileMode.Create, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(path08);
            sw.WriteLine(path07);
            sw.WriteLine(path06);
            sw.WriteLine(WorkSpacePath);
            sw.WriteLine(EFDCPath);
            sw.WriteLine(path09);
            sw.Close();
            fs.Close();

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM