(精华)2020年8月12日 C#基础知识点 文件相关操作
/// <summary>
/// 文件夹 文件管理
/// 配置文件:AdO.Net 数据库查询;
/// IOC配置文件
/// </summary>
public class MyIO
{<!-- -->
/// <summary>
/// 配置绝对路径
/// </summary>
private static string LogPath = ConfigurationManager.AppSettings["LogPath"];
private static string LogMovePath = ConfigurationManager.AppSettings["LogMovePath"];
/// <summary>
/// 获取当前程序路径
/// </summary>
private static string LogPath2 = AppDomain.CurrentDomain.BaseDirectory;
/// <summary>
/// 读取文件夹 文件信息
/// </summary>
public static void Show()
{<!-- -->
{<!-- -->
//--------------------------------------核查文件夹操作------------------------------------------
//检测文件夹是否存在
if (!Directory.Exists(LogPath))
{<!-- -->
}
//获取文件夹信息
DirectoryInfo directory = new DirectoryInfo(LogPath);
Console.WriteLine(string.Format("{0} {1} {2}", directory.FullName, directory.CreationTime, directory.LastWriteTime));
//检测文件是否存在
if (!File.Exists(Path.Combine(LogPath, "info.txt")))
{<!-- -->
}
//获取文件信息
FileInfo fileInfo = new FileInfo(Path.Combine(LogPath, "info.txt"));
Console.WriteLine(string.Format("{0} {1} {2}", fileInfo.FullName, fileInfo.CreationTime, fileInfo.LastWriteTime));
}
{<!-- -->
//------------------------------------文件夹相关操作-----------------------------------------------
if (!Directory.Exists(LogPath))
{<!-- -->
DirectoryInfo directoryInfo = Directory.CreateDirectory(LogPath);//一次性创建全部的子路径
Directory.Move(LogPath, LogMovePath);//移动原文件夹就不在了
Directory.Delete(LogMovePath);//删除文件夹
}
}
{<!-- -->
//-----------------------------------文件相关操作--------------------------------------------------
//合并文件路径
string fileName = Path.Combine(LogPath, "log.txt");
string fileNameCopy = Path.Combine(LogPath, "logCopy.txt");
string fileNameMove = Path.Combine(LogPath, "logMove.txt");
//判断文件是否存在
bool isExists = File.Exists(fileName);
if (!isExists)
{<!-- -->
//------------------------------------------写入文件和追加内容-------------------------------------
Directory.CreateDirectory(LogPath);//创建了文件夹之后,才能创建里面的文件
using (FileStream fileStream = File.Create(fileName))//打开文件流 (创建文件并写入)
{<!-- -->
string name = "第一种方式";
byte[] bytes = Encoding.Default.GetBytes(name);
fileStream.Write(bytes, 0, bytes.Length);
fileStream.Flush();
}
// 如果文件不存在就创建,如果存在就新建一个覆盖掉
using (FileStream fileStream = File.Create(fileName))//打开文件流 (创建文件并写入)
{<!-- -->
StreamWriter sw = new StreamWriter(fileStream);
sw.WriteLine("第二种方式");
sw.Flush();
}
using (StreamWriter sw = File.AppendText(fileName))//流写入器(创建/打开文件并写入)
{<!-- -->
string msg = "第三种方式";
sw.WriteLine(msg);
sw.Flush();
}
using (StreamWriter sw = File.AppendText(fileName))//流写入器(创建/打开文件并写入)
{<!-- -->
string name = "第四种方式";
byte[] bytes = Encoding.Default.GetBytes(name);
sw.BaseStream.Write(bytes, 0, bytes.Length);
sw.Flush();
}
//------------------------------------------读取文件-------------------------------------
//=======1==========
foreach (string result in File.ReadAllLines(fileName))
{<!-- -->
Console.WriteLine(result);
}
//=======2==========
string sResult = File.ReadAllText(fileName);
//=======3==========
Byte[] byteContent = File.ReadAllBytes(fileName);
string sResultByte = System.Text.Encoding.UTF8.GetString(byteContent);
//=======4==========
//文件太大,用工具去打开的时候,计算机直接卡死了
using (FileStream stream = File.OpenRead(fileName))//分批读取
{<!-- -->
int length = 5;
int result = 0;
do
{<!-- -->
byte[] bytes = new byte[length];
result = stream.Read(bytes, 0, 5);
for (int i = 0; i < result; i++)
{<!-- -->
Console.WriteLine(bytes[i].ToString());
}
}
while (length == result);
}
//------------------------------------------复制和删除文件-------------------------------------
File.Copy(fileName, fileNameCopy);
File.Move(fileName, fileNameMove);
File.Delete(fileNameCopy);
File.Delete(fileNameMove);//尽量不要delete
}
}
{<!-- -->
//--------------------获取所有驱动器信息---------------
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{<!-- -->
if (drive.IsReady)
Console.WriteLine("类型:{0} 卷标:{1} 名称:{2} 总空间:{3} 剩余空间:{4}", drive.DriveType, drive.VolumeLabel, drive.Name, drive.TotalSize, drive.TotalFreeSpace);
else
Console.WriteLine("类型:{0} is not ready", drive.DriveType);
}
}
{<!-- -->
//--------------------其他操作---------------
Console.WriteLine(Path.GetDirectoryName(LogPath)); //返回目录名,需要注意路径末尾是否有反斜杠对结果是有影响的
Console.WriteLine(Path.GetDirectoryName(@"d:\\abc")); //将返回 d:\
Console.WriteLine(Path.GetDirectoryName(@"d:\\abc\"));// 将返回 d:\abc
Console.WriteLine(Path.GetRandomFileName());//将返回随机的文件名
Console.WriteLine(Path.GetFileNameWithoutExtension("d:\\abc.txt"));// 将返回abc
Console.WriteLine(Path.GetInvalidPathChars());// 将返回禁止在路径中使用的字符
Console.WriteLine(Path.GetInvalidFileNameChars());//将返回禁止在文件名中使用的字符
Console.WriteLine(Path.Combine(LogPath, "log.txt"));//合并两个路径
}
}
/// <summary>
/// 关于文件异常处理
/// 1 try catch旨在上端使用,保证对用户的展示
/// 2 下端不要吞掉异常,隐藏错误是没有意义的,抓住再throw也没意义
/// 3 除非这个异常对流程没有影响或者你要单独处理这个异常
/// </summary>
/// <param name="msg"></param>
public static void Log(string msg)
{<!-- -->
StreamWriter sw = null;
try
{<!-- -->
string fileName = "log.txt";
string totalPath = Path.Combine(LogPath, fileName);
if (!Directory.Exists(LogPath))
{<!-- -->
Directory.CreateDirectory(LogPath);
}
sw = File.AppendText(totalPath); // 如果文件不存在,就新建一个文件,然后写入内容
sw.WriteLine(string.Format("{0}:{1}", DateTime.Now, msg));
sw.WriteLine("***************************************************");
}
catch (Exception ex)
{<!-- -->
Console.WriteLine(ex.Message);//log //建议大家在写日志的时候,把异常吞掉;
//throw ex;
//throw new exception("这里异常");
}
finally//无论是否发生异常 全都都会执行
{<!-- -->
if (sw != null)
{<!-- -->
sw.Flush();
sw.Close();
sw.Dispose();
}
}
}
}