C# 从txt文件读取字符串,再分割出来入table, 方便做批量操作小工具

 public DataTable gettxt()
        {
            #region 读取字符串
            StreamReader sr = new StreamReader("C:\\Users\\Lenovo\\Desktop\\111.txt", Encoding.GetEncoding("GB2312"));
            string txt = sr.ReadToEnd().Replace("\r\n", "-");
            string[] nodes = txt.Split('-');
            #endregion
            #region 填充DataTable
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(string));
            foreach (string node in nodes)
            {
                string[] strs = node.Split(',');
                DataRow dr = dt.NewRow();
                dr["ID"] = strs[0];
                dt.Rows.Add(dr);
            }
            #endregion
            sr.Close();
            return dt;
        }

你可能感兴趣的:(入门)