ADO 输入输出文本及获取指定字符串

---恢复内容开始---

       1.获取文本:声明别量,指定文本路径,获取文本内容。

string Text=System.IO.File.ReadAllText(@"C:\xxx\xxx\xxx\xxx.xxx//文本名称及后缀名");

Console.WriteLine(Text);//输出文本内容

       2.将内容写入文本

            string txtresult;//定义变量
   

            using (StreamReader sr = new StreamReader(@"c:\users\administrator\desktop\曲霉菌半乳甘露聚糖检测试剂盒(elisa法)_2019-05-05.csv", Encoding.Default))//指定文本地址获取值
            {
                txtresult = sr.ReadToEnd();//读取字符将字符传给变量
            }

            string[] arrsampledata = Regex.Split(txtresult, "\r\n", RegexOptions.None);//调整格式换行
            Console.WriteLine(txtresult);//输出变量值
            Console.ReadKey();
            
            System.IO.File.WriteAllText("e:/6.csv", txtresult, Encoding.Default)//写入文本内容及指定地址,转换文本格式
Console.WriteLine("写入成功!");//提示成功
Console.ReadKey();//按回车继续

       3.获取字符串位置,指定字符串输出

       int hqnum =Text.IndexOf("指定字符");//指定字符获取位置,传给变量

       string hqz = text.Substring(hqnum + 28, 10);//以指定字符下标开始后,获取固定的数值10

 

        4.通过条件循环按行获取文本值

 string line = string.Empty;//定义空字符串给到变量
 List<string> lines = new List<string>();//初始化数组
 using (StreamReader reader = new StreamReader(@"C:\Users\Administrator\Desktop\xxx.txt"))//获取桌面文本
    {
 line = reader.ReadLine();
  while (line != "" && line != null)//条件循环
       {
          lines.Add(line);//添加值到数组
          Console.WriteLine(line);
          line = reader.ReadLine();
       }

     }

 

posted @ 2019-06-25 10:03  Marco.Polo  阅读(259)  评论(0)    收藏  举报