热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> .Net编程>>正文内容

C# 使用List泛型读取和保存文本文件

发布时间:2010年03月06日点击数: 佚名
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5.  
  6. using System.Management;//添加引用 
  7. using System.IO; 
  8.  
  9. namespace ConsoleApplication1 
  10.     class Program 
  11.     { 
  12.         static void Main(string[] args) 
  13.         { 
  14.             //测试代码:  
  15.             test mgr = new test(); 
  16.             mgr.WriteListToTextFile(mgr.GetUserNames(), @"c:\test.txt"); //测试生成新的Txt文件 
  17.             List<string> list = mgr.ReadTextFileToList(@"C:\test.txt");//记取字符串  
  18.             foreach (string s in list) Console.WriteLine(s); //显示出来′  
  19.             Console.ReadKey(); //按′任一键关闭Console  
  20.           
  21.             /* 
  22.              * 结果: 
  23.              *  JUQI\ASPNET 
  24.                 JUQI\capable 
  25.                 JUQI\Guest 
  26.                 JUQI\itd0300166 
  27.                 JUQI\IUSR_PLSH166 
  28.                 JUQI\IWAM_PLSH166 
  29.                 JUQI\SUPPORT_388945a0 
  30.              *  
  31.              * */ 
  32.  
  33.         } 
  34.     } 
  35.         public class test 
  36.         { 
  37.  
  38.             /// <summary> 
  39.             /// 获取本机上的用户帐户 
  40.             /// </summary> 
  41.             /// <param name="domainName"></param> 
  42.             /// <returns></returns> 
  43.             public  List<String> GetUserNames() 
  44.             { 
  45.                 ManagementClass mcs = new ManagementClass("Win32_UserAccount"); 
  46.                 ManagementObjectCollection moc = mcs.GetInstances(); 
  47.                 List<String> lstNames = new List<string>(); 
  48.                 foreach (ManagementObject mo in moc) 
  49.                 { 
  50.                     lstNames.Add(Environment.UserDomainName + "\\" + mo.GetPropertyValue("Name").ToString()); 
  51.                     //domainName = mo.GetPropertyValue("Domain").ToString(); 
  52.                 } 
  53.                 return lstNames; 
  54.             } 
  55.  
  56.  
  57.             //将List转换为TXT文件 
  58.             public void WriteListToTextFile(List<string> list, string txtFile) 
  59.             { 
  60.                 //创建一个文件流,用以写入或者创建一个StreamWriter  
  61.                 FileStream fs = new FileStream(txtFile, FileMode.OpenOrCreate, FileAccess.Write); 
  62.                 StreamWriter sw = new StreamWriter(fs); 
  63.                 sw.Flush(); 
  64.                 // 使用StreamWriter来往文件中写入内容  
  65.                 sw.BaseStream.Seek(0, SeekOrigin.Begin); 
  66.                 for (int i = 0; i < list.Count; i++) sw.WriteLine(list[i]); 
  67.                 //关闭此文件t  
  68.                 sw.Flush(); 
  69.                 sw.Close(); 
  70.                 fs.Close(); 
  71.             } 
  72.  
  73.  
  74.             //读取文本文件转换为List  
  75.             public List<string> ReadTextFileToList(string fileName) 
  76.             { 
  77.                 FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); 
  78.                 List<string> list = new List<string>(); 
  79.                 StreamReader sr = new StreamReader(fs); 
  80.                 //使用StreamReader类来读取文件  
  81.                 sr.BaseStream.Seek(0, SeekOrigin.Begin); 
  82.                 // 从数据流中读取每一行,直到文件的最后一行 
  83.                 string tmp = sr.ReadLine(); 
  84.                 while (tmp != null
  85.                 { 
  86.                     list.Add(tmp); 
  87.                     tmp = sr.ReadLine(); 
  88.                 } 
  89.                 //关闭此StreamReader对象  
  90.                 sr.Close(); 
  91.                 fs.Close(); 
  92.                 return list; 
  93.             } 
  94.         } 
  95.  
  96.     } 

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作