using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Management;//添加引用 using System.IO; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //测试代码: test mgr = new test(); mgr.WriteListToTextFile(mgr.GetUserNames(), @"c:\test.txt"); //测试生成新的Txt文件 List<string> list = mgr.ReadTextFileToList(@"C:\test.txt");//记取字符串 foreach (string s in list) Console.WriteLine(s); //显示出来′ Console.ReadKey(); //按′任一键关闭Console /* * 结果:
* JUQI\ASPNET JUQI\capable JUQI\Guest JUQI\itd0300166 JUQI\IUSR_PLSH166 JUQI\IWAM_PLSH166 JUQI\SUPPORT_388945a0 * * */ } } public class test { /// <summary> /// 获取本机上的用户帐户
/// </summary> /// <param name="domainName"></param> /// <returns></returns> public ListStringManagementClass new ManagementClass"Win32_UserAccount"ManagementObjectCollection ListStringnew Liststringforeach ManagementObject in Environment"\\" "Name"//domainName = mo.GetPropertyValue("Domain").ToString(); return //将List转换为TXT文件 public void Liststringstring //创建一个文件流,用以写入或者创建一个StreamWriter FileStream new FileStreamFileModeFileAccessStreamWriter new StreamWriter// 使用StreamWriter来往文件中写入内容 0SeekOriginfor int 0//关闭此文件t //读取文本文件转换为List public Liststringstring FileStream new FileStreamFileModeFileAccessListstringnew ListstringStreamReader new StreamReader//使用StreamReader类来读取文件 0SeekOrigin// 从数据流中读取每一行,直到文件的最后一行 string while null//关闭此StreamReader对象 return
}
本文转自曾祥展博客园博客,原文链接:http://www.cnblogs.com/zengxiangzhan/archive/2010/01/30/1659674.html,如需转载请自行联系原作者