文件操作:
1.文件操作类有两个,File类和FileInfo类。File类的方法都是静态的,所以在跨线程操作时不会出现问题。FileInfo类需要和特定文件关联来实例化,操作大量文件时效率较高。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
File类文件操作
class Program
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
static void Main(string[] args)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
File.Copy("C:\\SucDir\\banner.bmp", "C:\\ToDir\\banner.bmp", true);
if (File.Exists("C:\\SucDir\\banner.bmp"))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ // File.Delete("C:\\SucDir\\banner.bmp");
}
File.Move("C:\\SucDir\\banner.bmp", "C:\\ToDir\\banner.bmp");
//File.Replace第三个参数用来设置被替换文件备份到哪个路径,null表示不备份
File.Replace("C:\\SucDir\\banner.bmp", "C:\\ToDir\\banner.bmp", null);
File.Create("C:\\SucDir\\banner.bmp");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void CopyFile(string srcFile, string desFile)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//判断文件名是否正确
if (srcFile == string.Empty)
throw new IOException("源文件名不能为空!");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (desFile == string.Empty)
throw new IOException("目标文件名不能为空!");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (!File.Exists(srcFile))
throw new IOException("源文件不存在!");
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (File.Exists(desFile))
Console.WriteLine("目标文件已经存在,将被覆盖!");
File.Copy(srcFile, desFile, true);
}
catch (IOException e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ throw e;//文件是重要资源,操作时一定要进行异常捕捉处理 }
}
}
2.得到或修改文件创建时间,使用GetCreationTime和SetCreationTime。 得到和修改文件属性,使用GetAttributes和SetAttributes。注意:GetAttributes返回的是FileAttributes这个枚举,所以还需进行位运算
//Archive、Hidden、ReadOnly、Normal
if((File.GetAttributes(filepath) & FileAttributes.Hidden)==FileAttributes .Hidden){}
File.SetAttributes(filepath,FileAttributes.Archive);
文件夹操作:
1.Directory类的所有方法都是静态的。打算多次重复操作某个文件夹,建议使用DirectoryInfo。
读写文件:
1.用File类创建FileStream对象,用FileStream类创建和操作文件流FileStream对象。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
namespace Test
{
class Program
{
static void Main(string[] args)
{
UseFileStreamWrite();
UseFileStreamRead();
}
//使用FileStream写入文件
public static void UseFileStreamWrite()
{
using (FileStream fs = File.Open("C:\\Log.txt", FileMode.Append))
{
//在写入之前首先要把字符串按照默认编码方式转换为字节数组
byte[] buffer = System.Text.Encoding.Default.GetBytes("Hello World!");
fs.Write(buffer, 0, buffer.Length);
fs.Close();
}
}
//使用FileStream读入文件并显示出来
public static void UseFileStreamRead()
{
using (FileStream fs = File.OpenRead("C:\\Log.txt"))
{
byte[] buffer = new byte[50];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
//将字节数组按默认编码方式解码成字符串并打印输出
string svalue = System.Text.Encoding.Default.GetString(buffer);
Console.WriteLine(svalue);
}
}
}
}
2.汉字由两个ASCII码来实现。区码对应ASCII码是“160+区码数字”,位码对应ASCII码是“160+位码数字”
string s="啊";
byte [] b=System.Text .Encoder.Default.GetBytes(s);
System.Console.WriteLine (b[0]-160);
System.Console.WriteLine (b[1]-160);
3.
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
简单记事本
namespace A9_4_2_2
{
public partial class FrmEdit : Form
{
public FrmEdit()
{
InitializeComponent();
}
private void btnOpen_Click(object sender, EventArgs e)
{
using (StreamReader sr = new StreamReader(this.txtPath.Text))
{
this.txtEdit.Text = sr.ReadToEnd();
sr.Close();
}
//using (FileStream fs = File.OpenRead(this.txtPath.Text))
//{
// byte[] buffer = new byte[255];
// //每次读取255个字节直到读完为止
// while (fs.Read(buffer, 0, buffer.Length) > 0)
// this.txtEdit.Text += System.Text.Encoding.Default.GetString(buffer);
//fs.Close();
//将字节数组转换成字符串并在编辑框中显示
//}
}
private void btnSave_Click(object sender, EventArgs e)
{
using (StreamWriter sw = new StreamWriter(this.txtPath.Text))
{
sw.WriteLine(this.txtEdit.Text);
sw.Close();
}
/*
using (FileStream fs = File.Open(this.txtPath.Text, FileMode.Append))
{
//在写之前首先要把字符串转换为字节数组
byte[] buffer = System.Text.Encoding.Default.GetBytes(this.txtEdit.Text);
fs.Write(buffer, 0, buffer.Length);
fs.Close();
}
*/
}
}
}