赞
踩
目录
常用函数:
Directory.Exists(path) -- 此文件夹是否存在
Directory.CreateDirectory(path) -- 创建文件夹
Directory.Move(path_a, path_b) -- 文件夹剪切
Directory.Delete(path) -- 只可删除空文件夹,若删除非空文件夹,会报异常
Directory.Delete(path, true) -- 删除文件夹,包括里面的文档
例程:
- internal class Program
- {
- static void Main(string[] args)
- {
- string path_A = @"C:\\Temp\\A";
- string path_B = @"C:\\Temp\\B";
-
- if (!Directory.Exists(path_A)) //若此文件夹不存在
- {
- Directory.CreateDirectory(path_A); //创建此文件夹
- Directory.Move(path_A, path_B); //A文件夹剪切成B
- Directory.Delete(path_B, true); //删除B文件夹
- }
- }
- }
常用函数:
Path.GetPathRoot(path) -- 获取根目录
Path.GetDirectoryName(path) -- 获取文件路径名
Path.GetFileNameWithoutExtension(path) -- 获取文件名 不包含后缀
Path.GetExtension(path) -- 获取文件后缀
Path.GetFileName(path) -- 获取文件名 包含后缀
Path.GetFullPath(path) -- 获取文件绝对路径
Path.ChangeExtension(path, "text") -- 修改文件后缀
Path.GetExtension(path) -- 获取文件后缀
Path.Combine("C:\\搜狗浏览器\\", "aaaa.txt") -- 合并文件路径Path.GetRandomFileName() -- 获取随机文件名+后缀
Path.PathSeparator -- 分隔符
Path.VolumeSeparatorChar -- 冒号
Path.AltDirectorySeparatorChar -- 反斜杠
例程:
- internal class Program
- {
- static void Main(string[] args)
- {
- string path = @"C:\搜狗浏览器\ldinst-41.exe.ttd";
-
- //获取根目录
- Console.WriteLine(Path.GetPathRoot(path)); //结果显示: C:\
-
- //获取文件路径名
- Console.WriteLine(Path.GetDirectoryName(path)); //结果显示: C:\搜狗浏览器
-
- //获取文件名 不包含后缀
- Console.WriteLine(Path.GetFileNameWithoutExtension(path));//结果显示: ldinst-41.exe
-
- //获取文件后缀
- Console.WriteLine(Path.GetExtension(path));//结果显示: .ttd
-
- //获取文件名 包含后缀
- Console.WriteLine( Path.GetFileName(path)); //结果显示: ldinst-41.exe.ttd
-
- //获取绝对路径
- Console.WriteLine(Path.GetFullPath(path));//结果显示: C:\搜狗浏览器\ldinst-41.exe.ttd
-
- //修改文件后缀
- Console.WriteLine(Path.ChangeExtension(path, "text"));//结果显示: C:\搜狗浏览器\ldinst-41.exe.text
-
- //合并路径
- Console.WriteLine(Path.Combine(Path.GetDirectoryName(path), "aaaa.txt"));//结果显示: C:\搜狗浏览器\aaaa.txt
-
- //获取随机文件名+后缀
- Console.WriteLine(Path.GetRandomFileName()); //结果显示: vjzle4pq.1v0
-
- Console.WriteLine(Path.PathSeparator); //分隔符
- Console.WriteLine(Path.VolumeSeparatorChar);//冒号
- Console.WriteLine(Path.AltDirectorySeparatorChar); //反斜杠
-
- }
- }

常用函数:
File.Create(path) -- 文档创建并清空
File.Copy(path_1,path_2) -- 文档复制
File.Delete(path_2) -- 文档删除
File.Move(path_1,path_2) -- 文档剪切
File.AppendText(path) -- 文档内容追加
File.ReadAllLine(path) -- 文档读取所有行
File.ReadAllText(path) -- 读取整个文档FileStream.Write(bytes,0,bytes.Length) -- 一个个写
FileStream.Flush() -- 刷新StreamWriter.WriteLine(text) -- 写一行
StreamWrite.BaseStream.Write(bytes,0,bytes.Length) -- 一个个写
StreamWrite.Flush() -- 刷新
例程:
- internal class Program
- {
- static void Main(string[] args)
- {
-
- string path = "C:\\Temp\\A.txt";
- using (FileStream fs = File.Create(path))//创建并清空原有数据
- {
- byte[] bytes = Encoding.UTF8.GetBytes("我最最最爱你,你不知道吗\r\n");
- fs.Write(bytes, 0, bytes.Length); //一个个写
- fs.Flush();
-
- }
- using (StreamWriter sw = File.AppendText(path)) //流写入 -- 追加
- {
- string text = "我是你.......";
- sw.WriteLine(text);
-
- byte[] bytes = Encoding.UTF8.GetBytes("eeeeeeeee\r\n");
- sw.BaseStream.Write(bytes, 0, bytes.Length); //一个个写
-
- sw.Flush();
- }
- //File.OpenRead(path);等价
- using (FileStream fs = new FileStream(path, FileMode.Open)) //分批读取
- {
- byte[] bytes = new byte[1 * 1024];
- int count = fs.Read(bytes, 0, bytes.Length);
- string date = Encoding.UTF8.GetString(bytes, 0, count);
- Console.WriteLine(date);
- }
-
- foreach (string item in File.ReadAllLines(path)) //读取所有行,遍历
- {
- Console.WriteLine(item);
- }
-
- Console.WriteLine(File.ReadAllText(path)); //读取整个文本
-
- string path_1 = "C:\\Temp\\A.txt";
- string path_2 = "C:\\Temp\\B.txt";
- File.Copy(path_1, path_2); //文档复制
- File.Delete(path_2); //文档删除
- File.Move(path_1, path_2); //文档剪切
-
- }
- }

常用函数:
DriveInfo.GetDrives() -- 获取当前所有磁盘
drive.IsReady -- 磁盘是否可用
drive.DriveType -- 获取磁盘类型
drive.Name -- 磁盘名称
drive.TotalSize -- 磁盘总空间
drive.AvailableFreeSpace -- 磁盘剩余空间
例程:
- internal class Program
- {
- static void Main(string[] args)
- {
- foreach (DriveInfo drive in DriveInfo.GetDrives())
- {
- if (drive.IsReady)
- {
- Console.WriteLine($"类型:{drive.DriveType}" +
- $"名称:{drive.Name}" +
- $"总空间:{drive.TotalSize / 1024 / 1024 / 1024}G " +
- $"剩余空间:{drive.AvailableFreeSpace / 1024 / 1024 / 1024} G");
- }
- else
- {
- Console.WriteLine($"类型:{drive.DriveType} is not ready");
- }
- }
- }
- }

常用函数:
二进制序列化
BinaryFormatter.Serialize(stream, text) -- 序列化成二进制,写入文档
BinaryFormatter.Deserialize(stream) --反序列化读出文档
XML序列化
XmlSerializer.Serialize(stream, text) -- 序列化成XML格式,写入文档
XmlSerializer.Deserialize(stream) --反序列化读取
Json序列化
JsonConvert.SerializeObject(text) -- 序列化
JsonConvert.DeserializeObject(result) -- 反序列化
例程:
- internal class Program
- {
- static void Main(string[] args)
- {
- string path = @"C:\Temp\C.txt";
- string text = "凄凄切切QQ群www";
-
- //---------------二进制序列化------------------------
- using (Stream stream = File.OpenWrite(path))
- {
- BinaryFormatter bf = new BinaryFormatter();
- //序列化文档
- bf.Serialize(stream, text);
- }
- using (Stream stream = File.OpenRead(path))
- {
- BinaryFormatter bf = new BinaryFormatter();
- //反序列化读取
- Console.WriteLine(bf.Deserialize(stream));
- }
-
- //---------------XML序列化------------------------
- using (Stream stream = File.OpenWrite(path))
- {
- XmlSerializer serializer = new XmlSerializer(typeof(string));
- //序列化文档
- serializer.Serialize(stream, text);
- }
- using (Stream stream = File.OpenRead(path))
- {
- XmlSerializer serializer = new XmlSerializer(typeof(string));
- //反序列化读取
- Console.WriteLine(serializer.Deserialize(stream));
- }
- //---------------Json序列化------------------------
- string result = JsonConvert.SerializeObject(text); //序列化
- Console.WriteLine(result);
-
- Console.WriteLine(JsonConvert.DeserializeObject(result)); //反序列化
- }
- }

如有错误,烦请批评指正
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。