一.文本文件内容读写操作
1.导入命名空间, System.IO;
2.创建文件流:FileStream
string path=txtFile.Text.Trim();
FileStream fs=new FileStream(path,FileMode.Open)
3.创建文件读取器:StreamReader
StreamReader reader=new StreamReader(fs);
4.读取或者写入文件内容
string context=reader.ReadToEnd();
txtContext.Text=context;
5.关闭读取器,再关闭文件流
reader.Close();
fs.Close();
FileMode:文件打开方式
1)CreateNew:创建新文件,如果存在,则出现异常。
2)Create:创建新文件,如果存在,则覆盖内容。
3)Open:打开一个文件,如果不存在,则出现异常。
4)Append:追加内容
文件选择对话框:
控件:openFileDialog
打开对话框:openFileDialog.ShowDialog();
返回地址:openFileDialog.FileName;
二.文件和文件夹的操作
1.文件操作
1)判断文件是否操作
2)复制
3)剪切
4)删除
5)创建新文件
...
三.静态类与非静态的区别
1.静态类用static修饰,非静态类无static修饰。
2.静态类中只存在静态的,非静态类中可存在普通方法和静态方法。
3.静态类不能new对象,非静态类可以new对象。
4.静态成员只能用类名调用,非静态成员用对象调用。
string path = Path.Combine(filePath, fileName);
FileStream fsRead = new FileStream(path, FileMode.Create, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fsRead);
byte[] bytes = new byte[files.ContentLength];
files.InputStream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
files.InputStream.Seek(0, SeekOrigin.Begin);
// 把 byte[] 写入文件
bw.Write(bytes);
bw.Close();
fsRead.Close();