代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
使用List泛型读取和保存文本文件
作者:
Dezai.CN
/ 发布于
2011/3/15
/
543
<div> <pre class="code"><span style="color: blue">using </span>System; <span style="color: blue">using </span>System.Collections.Generic; <span style="color: blue">using </span>System.Linq; <span style="color: blue">using </span>System.Text; <span style="color: blue">using </span>System.Management;<span style="color: green">//添加引用 </span><span style="color: blue">using </span>System.IO; <span style="color: blue">namespace </span>ConsoleApplication1 { <span style="color: blue">class </span><span style="color: rgb(43,145,175)">Program </span>{ <span style="color: blue">static void </span>Main(<span style="color: blue">string</span>[] args) { <span style="color: green">//测试代码: </span><span style="color: rgb(43,145,175)">test </span>mgr = <span style="color: blue">new </span><span style="color: rgb(43,145,175)">test</span>(); mgr.WriteListToTextFile(mgr.GetUserNames(), <span style="color: rgb(163,21,21)">@"c:\test.txt"</span>); <span style="color: green">//测试生成新的Txt文件 </span><span style="color: rgb(43,145,175)">List</span><<span style="color: blue">string</span>> list = mgr.ReadTextFileToList(<span style="color: rgb(163,21,21)">@"C:\test.txt"</span>);<span style="color: green">//记取字符串 </span><span style="color: blue">foreach </span>(<span style="color: blue">string </span>s <span style="color: blue">in </span>list) <span style="color: rgb(43,145,175)">Console</span>.WriteLine(s); <span style="color: green">//显示出来′ </span><span style="color: rgb(43,145,175)">Console</span>.ReadKey(); <span style="color: green">//按′任一键关闭Console /* * 结果:</span></pre> <pre class="code"><span style="color: green"> * JUQI\ASPNET JUQI\capable JUQI\Guest JUQI\itd0300166 JUQI\IUSR_PLSH166 JUQI\IWAM_PLSH166 JUQI\SUPPORT_388945a0 * * */ </span>} } <span style="color: blue">public class </span><span style="color: rgb(43,145,175)">test </span>{ <span style="color: gray">/// <summary> /// </span><span style="color: green">获取本机上的用户帐户</span></pre> <pre class="code"><span style="color: green"> </span><span style="color: gray">/// </summary> /// <param name="domainName"></param> /// <returns></returns> </span><span style="color: blue">public </span><span style="color: rgb(43,145,175)">List</span><<span style="color: rgb(43,145,175)">String</span>> GetUserNames() { <span style="color: rgb(43,145,175)">ManagementClass </span>mcs = <span style="color: blue">new </span><span style="color: rgb(43,145,175)">ManagementClass</span>(<span style="color: rgb(163,21,21)">"Win32_UserAccount"</span>); <span style="color: rgb(43,145,175)">ManagementObjectCollection </span>moc = mcs.GetInstances(); <span style="color: rgb(43,145,175)">List</span><<span style="color: rgb(43,145,175)">String</span>> lstNames = <span style="color: blue">new </span><span style="color: rgb(43,145,175)">List</span><<span style="color: blue">string</span>>(); <span style="color: blue">foreach </span>(<span style="color: rgb(43,145,175)">ManagementObject </span>mo <span style="color: blue">in </span>moc) { lstNames.Add(<span style="color: rgb(43,145,175)">Environment</span>.UserDomainName + <span style="color: rgb(163,21,21)">"\\" </span>+ mo.GetPropertyValue(<span style="color: rgb(163,21,21)">"Name"</span>).ToString()); <span style="color: green">//domainName = mo.GetPropertyValue("Domain").ToString(); </span>} <span style="color: blue">return </span>lstNames; } <span style="color: green">//将List转换为TXT文件 </span><span style="color: blue">public void </span>WriteListToTextFile(<span style="color: rgb(43,145,175)">List</span><<span style="color: blue">string</span>> list, <span style="color: blue">string </span>txtFile) { <span style="color: green">//创建一个文件流,用以写入或者创建一个StreamWriter </span><span style="color: rgb(43,145,175)">FileStream </span>fs = <span style="color: blue">new </span><span style="color: rgb(43,145,175)">FileStream</span>(txtFile, <span style="color: rgb(43,145,175)">FileMode</span>.OpenOrCreate, <span style="color: rgb(43,145,175)">FileAccess</span>.Write); <span style="color: rgb(43,145,175)">StreamWriter </span>sw = <span style="color: blue">new </span><span style="color: rgb(43,145,175)">StreamWriter</span>(fs); sw.Flush(); <span style="color: green">// 使用StreamWriter来往文件中写入内容 </span>sw.BaseStream.Seek(<span style="color: brown">0</span>, <span style="color: rgb(43,145,175)">SeekOrigin</span>.Begin); <span style="color: blue">for </span>(<span style="color: blue">int </span>i = <span style="color: brown">0</span>; i < list.Count; i++) sw.WriteLine(list[i]); <span style="color: green">//关闭此文件t </span>sw.Flush(); sw.Close(); fs.Close(); } <span style="color: green">//读取文本文件转换为List </span><span style="color: blue">public </span><span style="color: rgb(43,145,175)">List</span><<span style="color: blue">string</span>> ReadTextFileToList(<span style="color: blue">string </span>fileName) { <span style="color: rgb(43,145,175)">FileStream </span>fs = <span style="color: blue">new </span><span style="color: rgb(43,145,175)">FileStream</span>(fileName, <span style="color: rgb(43,145,175)">FileMode</span>.Open, <span style="color: rgb(43,145,175)">FileAccess</span>.Read); <span style="color: rgb(43,145,175)">List</span><<span style="color: blue">string</span>> list = <span style="color: blue">new </span><span style="color: rgb(43,145,175)">List</span><<span style="color: blue">string</span>>(); <span style="color: rgb(43,145,175)">StreamReader </span>sr = <span style="color: blue">new </span><span style="color: rgb(43,145,175)">StreamReader</span>(fs); <span style="color: green">//使用StreamReader类来读取文件 </span>sr.BaseStream.Seek(<span style="color: brown">0</span>, <span style="color: rgb(43,145,175)">SeekOrigin</span>.Begin); <span style="color: green">// 从数据流中读取每一行,直到文件的最后一行 </span><span style="color: blue">string </span>tmp = sr.ReadLine(); <span style="color: blue">while </span>(tmp != <span style="color: blue">null</span>) { list.Add(tmp); tmp = sr.ReadLine(); } <span style="color: green">//关闭此StreamReader对象 </span>sr.Close(); fs.Close(); <span style="color: blue">return </span>list; } } }</pre> </div>
试试其它关键字
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3