首页 > 开发 > 综合 > 正文

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

2024-07-21 02:25:27
字体:
来源:转载
供稿:网友


按行读写文件
判断文件是否存在:file.exists(string filepath)
判断目录是否存在:directory.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;


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表