扫描二维码关注

首页 APP开发小程序开发 微信公众号 网站建设 营销推广 经典案列 产品服务 关于我们

“学习不仅是掌握知识”

向书本学习,还要向实践学习、向生活学习。消化已有知识,
而且要力求有所发现、有所发明、有所创造

.NET文件操作

2019/3/24 8:18:11

.NET文件操作

文件的建立

源代码:
private void Button1_Click(object sender, System.EventArgs e)
{
//string wenjian;
//wenjian = filename.Text;
if (this.filename.Text.ToString().Trim()=="")
{
this.body.Text = "********";
}
else
{
try
{
string thisfilename=filename.Text.ToString();
FileStream fsMyfile = new FileStream(thisfilename,FileMode.Create,FileAccess.ReadWrite);
fsMyfile.Close();
this.body.Text = "********";
}
catch
{
//Console.WriteLine("{0} Second exception caught.", e);

this.body.Text = "********";

}
}

} 


文件的删除

源代码:


private void Button4_Click(object sender, System.EventArgs e)
{
if (this.filename.Text.ToString().Trim()=="")
{
this.body.Text = "********";
}
else
{
try
{
string thisfilename=filename.Text.ToString();

File.Delete(thisfilename);
this.body.Text = "********";

}
catch
{
//Console.WriteLine("{0} Second exception caught.", e);

this.body.Text = "********";

}


}

}

文件的读取

源代码:


private void Button2_Click(object sender, System.EventArgs e)

{
if (this.filename.Text.ToString().Trim()=="")
{
this.body.Text = "请选择文件名称!";
}
else
{
try
{


string thisfilename=filename.Text.ToString();
StreamReader srMyfile = new StreamReader(thisfilename);
srMyfile.BaseStream.Seek(0,SeekOrigin.Begin);
string sl;
sl = srMyfile.ReadToEnd();
this.body.Text = sl;
srMyfile.Close();
}
catch
{
//Console.WriteLine("{0} Second exception caught.", e);

this.body.Text = "此文件不存在!";

}

}

}

文件的写入

源代码:


private void Button3_Click(object sender, System.EventArgs e)
{
if (this.filename.Text.ToString().Trim()=="")
{
this.body.Text = "********";
}
else
{
try
{
string thisfilename=filename.Text.ToString();
StreamWriter swMyfile = new StreamWriter(thisfilename);
string xieru = body.Text.ToString();
swMyfile.WriteLine(xieru);
swMyfile.Flush();
swMyfile.Close();
this.body.Text = "********";

}
catch
{
//Console.WriteLine("{0} Second exception caught.", e);

this.body.Text = "********";

}


}
 


深圳市南山区南山街道南海大道西桂庙路北阳光华艺大厦1栋4F、4G-04

咨询电话:136 8237 6272
大客户咨询:139 0290 5075
业务QQ:2062128898
业务QQ:195006118
技术QQ:179981967

精锐软件

Copyright© 2018-2022 深圳精锐软件技术有限公司 All Rights Reserved. ICP备案号:粤ICP备18108116号-1 公安备案号:粤公网安备44030502003401号