版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
FileInfo、DirectoryInfo
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _026_文件操作_查看文件和文件夹信息 {
class Program {
static void Main(string[] args) {
FileInfo fileInfo = new FileInfo("TextFile1.txt");
Console.WriteLine(fileInfo.Exists);
//相对路径:就是找当前程序所在的路径
//FileInfo fileInfo = new FileInfo("TextFile1.txt");
//绝对路径:加上文件完整的路径名
//FileInfo fileInfo = new FileInfo(@"C:\Users\devsiki\Documents\Visual Studio 2012\Projects\学习csharp编程 高级篇\026-文件操作-查看文件和文件夹信息\bin\Debug\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(@"C:\Users\devsiki\Documents\Visual Studio 2012\Projects\学习csharp编程 高级篇\026-文件操作-查看文件和文件夹信息\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 _027_使用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", "hello sdf中国");
//File.WriteAllLines("textfile3.txt",new string[]{" sdfsdflk","213412","流口水的减肥"});
//复制图片
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;
using System.Xml;
namespace _028_使用FileStream读写文件 {
class Program {
static void Main(string[] args) {
//1,创建文件流 用来操作文件
//FileStream stream = new FileStream("TextFile1.txt",FileMode.Open);
////2, 读取或者写入数据
//byte[] data = new byte[1024];//数据容器
////1 byte = 1字节 1024byte=1KB 1024KB=1MB 1024MB = 1GB 1024GB=1T
//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);
}
}
writeStream.Close();
readStream.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 _029_使用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();
}
}
}