首页 > 代码库 > .Net常用技巧_C#写入和读取txt文件

.Net常用技巧_C#写入和读取txt文件

using System.Text.RegularExpressions;  //脱机数据导出        private void writeTxt(string str)        {            string strFileName = @"\Program Files\firexungengcard\FireXungeng.txt";            FileInfo file = new FileInfo(strFileName);            StreamWriter sWriter;            if (file.Exists)            {                sWriter = new StreamWriter(strFileName, true, Encoding.Default);                 sWriter.WriteLine(str);                sWriter.Close();            }            else            {                file.Create().Close();                sWriter = new StreamWriter(strFileName, true, Encoding.Default);                sWriter.WriteLine(str);                sWriter.Close();            }                                }          //返回脱机数据            private string[] readTxt()           {               string strFileName = @"\Program Files\firexungengcard\FireXungeng.txt";                FileInfo file = new FileInfo(strFileName);                if (file.Exists)                {                    string content = String.Empty;                    using (StreamReader sr = new StreamReader(strFileName))                    {                        content = sr.ReadToEnd();                    }                    //拆分字符串                     string[] strContentArray = Regex.Split(content, "\r\n", RegexOptions.IgnoreCase);                    file.Delete();                    return   strContentArray;                }                else                 {                    MessageBox.Show("你没有可导入的数据");                }                return null;           }