职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 2026|回复: 0

C#:文件的按行读/写及文件目录对话框的使用

[复制链接]
joe 发表于 2006-11-8 22:39 | 显示全部楼层 |阅读模式
按行读写文件
判断文件是否存在:File.Exists(string filePath)
判断目录是否存在irectory.Exists(\"D:\\\\LastestVersion\")

按行读取文件:
int fileCount=0;
// Open the file just specified such that no one else can use it.
StreamReader sr = new StreamReader(textBox1.Text.Trim());
while(sr.Peek() > -1)//StreamReader.Peek()返回下一个可用字符,但不使用它
{
listBox1.Items.Add(sr.ReadLine());
fileCount++;
}
sr.Close();

按行写入文件:
StreamWriter sw = new StreamWriter(\"D:\\\\result.txt\");
for(int i=0;i<10;i++)
{
sw.WriteLine(\"这是第\"+i.ToString()+\"行数据\");
}

文件目录对话框的使用
文件对话框即过滤条件的使用:
string resultFile=\"\";
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = \"D:\\\\Patch\" ;
openFileDialog1.Filter = \"All files (*.*)|*.*|txt files (*.txt)|*.txt\" ;
openFileDialog1.FilterIndex = 2 ;
openFileDialog1.RestoreDirectory = true ;
if(openFileDialog1.ShowDialog() == DialogResult.OK)
resultFile=openFileDialog1.FileName;

目录对话框的使用:
string resultFolder=\"\";
FolderBrowserDialog openFolderDialog1=new FolderBrowserDialog();
openFolderDialog1.RootFolder=Environment.SpecialFolder.MyComputer;
if(openFolderDialog1.ShowDialog()==DialogResult.OK)
resultFolder=openFolderDialog1.SelectedPath;
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-4-28 01:46 , Processed in 0.112341 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表