扫描二维码关注

首页 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 = "********";

}


}
 


长沙高新开发区谷苑路186号湖南大学科技园创业大厦429

咨询电话:0731-8225 2399
业务QQ:1020299919
大客户专线:139 7581 8321

友情链接:
Copyright© 2014-2024 湖南省鑫冠计算机系统有限公司 版权所有 ICP备案号:湘ICP备17013418号-1 公安备案号:湘公网安备43019002001544号