对文件和文件夹进行操作
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _031_文件操作
{
class Program
{
static void Main(string[] args)
{
//相对路径:就是找当前程序所在的路径
//FileInfo fileInfo = new FileInfo("TextFile1.txt");
//绝对路径:加上文件完整的路径名
//FileInfo fileInfo = new FileInfo(@"D:\VS工作空间\学习csharp编程 高级篇\031-文件操作\TextFile1.txt");
//Console.WriteLine(fileInfo.Exists);//判断该文件是否存在
//Console.WriteLine(fileInfo.Name);//文件名.后缀
//Console.WriteLine(fileInfo.Directory);//取得文件所在路径
//Console.WriteLine(fileInfo.Length);//取得文件大小字节数
//Console.WriteLine(fileInfo.IsReadOnly);
//fileInfo.Delete();//删除输出路径的文件
//fileInfo.CopyTo("tt.txt");//复制当前文件到指定目录下
//FileInfo fileInfo = new FileInfo("siki.txt");
if(fileInfo.Exists == false)
{
fileInfo.Create();//创建当前文件
}
//fileInfo.MoveTo("siki2.txt");//重命名操作
//文件夹操作(目录操作)
//DirectoryInfo dirInfo = new DirectoryInfo(@"D:\VS工作空间\学习csharp编程 高级篇\031-文件操作\bin\Debug");
查看Debug文件夹的信息
//Console.WriteLine(dirInfo.Exists);
//Console.WriteLine(dirInfo.Name);
//Console.WriteLine(dirInfo.Parent);
//Console.WriteLine(dirInfo.Root);
//Console.WriteLine(dirInfo.CreationTime);
//dirInfo.CreateSubdirectory("siki");
DirectoryInfo dirInfo = new DirectoryInfo("test");
if(dirInfo.Exists == false)
{
dirInfo.Create();//创建目录
}
Console.ReadKey();
}
}
}
使用File读写文件
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _032_使用File读写文件
{
class Program
{
static void Main(string[] args)
{
//string[] strArray = File.ReadAllLines("TextFile1.txt");
把每行文本读取成一个字符串,最后组成一个字符串的数组
//foreach(var s in strArray)
//{
// Console.WriteLine(s);
//}
//string s = File.ReadAllText("TextFile1.txt");
//Console.WriteLine(s);
//读取图片
//byte[] byteArray = File.ReadAllBytes("3.LINQ.png");
//foreach(var b in byteArray)
//{
// Console.Write(b);
//}
//File.WriteAllText("TextFile2.txt", "你好中国!");
//File.WriteAllLines("TextFile3.txt", new string[] { "sdasdas", "1232", "dasd213" });
byte[] data = File.ReadAllBytes("3.LINQ.png");
File.WriteAllBytes("4.png", data);//相当于复制图片
Console.ReadKey();
}
}
}
使用FileStream读写文件(擅长读取二进制的文件)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _033_使用FileSteam读写文件
{
class Program
{
static void Main(string[] args)
{
1.创建文件流,用来操作文件
//FileStream stream = new FileStream("TextFile1.txt",FileMode.Open);
2.读取或者写入数据
//byte[] data = new byte[1024];//数据容器
1byte = 1字节 1024byte = 1KB
//while (true)
//{
// int length = stream.Read(data, 0, data.Length);
// if(length == 0)
// {
// Console.WriteLine("读取结束");
// break;
// }
// for (int i = 0; i < length; i++)
// {
// Console.Write(data[i] + " ");
// }
//}
//使用filestream完成文件复制
//1.5MB = 1.5*1024KB = 2K多KB = 2K多*1000 byte
FileStream readStream = new FileStream("3.LINQ.png", FileMode.Open);
FileStream writeStream = new FileStream("LINQ副本.png", FileMode.Create);
byte[] data = new byte[1024];
while (true)
{
int length = readStream.Read(data, 0, data.Length);
if(length == 0)
{
Console.WriteLine("读取结束");
break;
}
else
{
writeStream.Write(data, 0, length);
}
}
readStream.Close();
writeStream.Close();
Console.ReadKey();
}
}
}
使用StreamReader和StreamWriter读写文本文件
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _034_使用StreamReader和StreamWriter读写文本文件
{
class Program
{
static void Main(string[] args)
{
//创建文本文件读取流
//StreamReader reader = new StreamReader("TextFile1.txt");
//while (true)//循环调用可以读取所有文件
//{
// string str = reader.ReadLine();//读取一行字符串
// if (str == null) break;
// Console.WriteLine(str);
//}
//string str = reader.ReadToEnd();//读取到文本的结尾(读取文本中所有的字符串)
//Console.WriteLine(str);
//while (true)
//{
// int res = reader.Read();
// if(res == -1)
// {
// break;
// }
// else
// {
// Console.Write((char)res);
// }
//}
//reader.Close();
//文件文本写入流
StreamWriter writer = new StreamWriter("textfile2.txt");
//如果文件存在,那么文件会被覆盖
while (true)
{
string message = Console.ReadLine();
if (message == "q")
break;
//writer.Write(message);//写入一个字符串
writer.WriteLine(message);//写入一个字符串并换行
}
writer.Close();
Console.ReadKey();
}
}
}