三大工具类:File,Directory,Path
静态类File:
//Append lines to a file, then closes the file.
public static void AppendAllLines(string path, IEnumerable<string> contents)
public static void AppendAllLines(string path, IEnumerable<string> contents, Encoding encoding)
//Opens a file, appends the specified string to the file, and then closes the file.
//If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.
public static void AppendAllText(string path, string contents)
public static void AppendAllText(string path, string contents, Encoding encoding)
//根据路径创建StreamWriter对象,以Append的方式添加文本字符
public static StreamWriter AppendText(string path)
//将现有文件复制到新文件。不允许覆盖同名文件。
public static void Copy(string sourceFileName, string destFileName)
public static void Copy(string sourceFileName, string destFileName, bool overwrite)
//根据指定的filepath,创建或重写一个文件
public static FileStream Create(string path)
public static FileStream Create(string path, int bufferSize)
//根据指定的filepath,创建或重写一个文件
public static StreamWriter CreateText(string path)
//加密文件,只有用于加密文件的帐户才能对其进行解密。
public static void Encrypt(string path)
public static void Decrypt(string path)
//Deletes the specified file.
public static void Delete(string path)
public static bool Exists(string path)
public static FileSecurity GetAccessControl(string path)
public static FileAttributes GetAttributes(string path)
public static DateTime GetCreationTime(string path)
public static DateTime GetLastAccessTime(string path)
public static DateTime GetLastWriteTime(string path)
//将指定的文件移动到新位置,并指定新文件名。
public static void Move(string sourceFileName, string destFileName)
//根据路径打开文件流
public static FileStream Open(string path, FileMode mode)
public static FileStream Open(string path, FileMode mode, FileAccess access)
public static FileStream OpenRead(string path)
public static StreamReader OpenText(string path)
public static FileStream OpenWrite(string path)
//根据路径返回文件的二进制数组
public static byte[] ReadAllBytes(string path)
//根据路径返回文件的所有文本字符行数据
public static string[] ReadAllLines(string path)
public static string[] ReadAllLines(string path, Encoding encoding)
public static IEnumerable<string> ReadLines(string path)
public static IEnumerable<string> ReadLines(string path, Encoding encoding)
//根据路径返回文件的所有文本字符
public static string ReadAllText(string path)
public static string ReadAllText(string path, Encoding encoding)
//覆盖指定的目标文件,同时可以备份目标的原文件
public static void Replace(string sourceFileName, string destinationFileName, string destinationBackupFileName)
//设置文件的特性值
public static void SetAttributes(string path, FileAttributes fileAttributes)
//设置创建时间?TODO测试
public static void SetCreationTime(string path, DateTime creationTime)
//设置最近的访问时间
public static void SetLastAccessTime(string path, DateTime lastAccessTime)
//设置最近的修改时间
public static void SetLastWriteTime(string path, DateTime lastWriteTime)
//写入二进制数据
public static void WriteAllBytes(string path, byte[] bytes)
//写入多行数据
public static void WriteAllLines(string path, string[] contents)
public static void WriteAllLines(string path, string[] contents, Encoding encoding)
public static void WriteAllLines(string path, IEnumerable<string> contents)
public static void WriteAllLines(string path, IEnumerable<string> contents, Encoding encoding)
//写入一段文本信息
public static void WriteAllText(string path, string contents)
public static void WriteAllText(string path, string contents, Encoding encoding)
File类是一个工具类,它可以便捷的通过文件路径,对文件进行一系列的操作。上一篇文章里面介绍了FileInfo类的用法,File类里面的很多功能和FileInfo提供的功能类似,并提供了更多的方法,通过查看.Net Framework源码,FileInfo类里面有调用File的方法,但是File里面引用到FileInfo类。
静态类Directory:
public static bool Exists(string path)
public static DirectoryInfo CreateDirectory(string path)
public static void Move(string sourceDirName, string destDirName)
public static void Delete(string path)
public static void Delete(string path, bool recursive)
public static DateTime GetCreationTime(string path)
public static DateTime GetLastAccessTime(string path)
public static DateTime GetLastWriteTime(string path)
public static void SetCreationTime(string path, DateTime creationTime)
public static void SetLastAccessTime(string path, DateTime lastAccessTime)
public static void SetLastWriteTime(string path, DateTime lastWriteTime)
//返回当前的Directory的path
public static string GetCurrentDirectory()
public static void SetCurrentDirectory(string path)
public static DirectoryInfo GetParent(string path)
public static string GetDirectoryRoot(string path)
public static string[] GetLogicalDrives()
public static IEnumerable<string> EnumerateDirectories(string path)
public static IEnumerable<string> EnumerateDirectories(string path, string searchPattern)
public static IEnumerable<string> EnumerateDirectories(string path, string searchPattern, SearchOption searchOption)
public static IEnumerable<string> EnumerateFiles(string path)
public static IEnumerable<string> EnumerateFiles(string path, string searchPattern)
public static IEnumerable<string> EnumerateFiles(string path, string searchPattern, SearchOption searchOption)
public static IEnumerable<string> EnumerateFileSystemEntries(string path)
public static IEnumerable<string> EnumerateFileSystemEntries(string path, string searchPattern)
public static IEnumerable<string> EnumerateFileSystemEntries(string path, string searchPattern, SearchOption searchOption)
public static string[] GetDirectories(string path)
public static string[] GetDirectories(string path, string searchPattern)
public static string[] GetDirectories(string path, string searchPattern, SearchOption searchOption)
public static string[] GetFiles(string path)
public static string[] GetFiles(string path, string searchPattern)
public static string[] GetFiles(string path, string searchPattern, SearchOption searchOption)
public static string[] GetFileSystemEntries(string path)
public static string[] GetFileSystemEntries(string path, string searchPattern)
public static string[] GetFileSystemEntries(string path, string searchPattern, SearchOption searchOption)
Directory类是一个工具类,它可以便捷的通过路径,对目录进行一系列的操作。上一篇文章里面介绍了DirectoryInfo类的用法,Directory类里面的很多功能和DirectoryInfo提供的功能类似,并提供了更多的方法,通过查看.Net Framework源码,DirectoryInfo类里面有调用Directory的方法,Directory里面也引用到DirectoryInfo类。这一行为和File的行为不太一致。
静态类Path:
它的属性:
readonly static char DirectorySeparatorChar;
readonly static char AltDirectorySeparatorChar;
readonly static char VolumeSeparatorChar;
readonly static char[] InvalidPathChars;
readonly static char PathSeparator;
方法:
public static bool IsPathRooted(string path)
public static bool HasExtension(string path)
public static string GetDirectoryName(string path)
public static string GetExtension(string path)
public static string GetFileName(string path)
public static string GetFileNameWithoutExtension(string path)
public static string GetFullPath(string path)
public static char[] GetInvalidFileNameChars()
public static char[] GetInvalidPathChars()
public static string GetPathRoot(string path)
public static string GetRandomFileName()
public static string GetTempFileName()
public static string GetTempPath()
//replace传入的path的后缀,返回新的path,仅仅是字符串操作而已
public static string ChangeExtension(string path, string extension)
public static string Combine(string path1, string path2)
public static string Combine(params string[] paths)
Combine方法的.Net Framework源码:
internal static string CombineNoChecks(string path1, string path2)
{
if (path2.Length == 0)
{
return path1;
}
if (path1.Length == 0)
{
return path2;
}
if (Path.IsPathRooted(path2))
{
return path2;
}
char chr = path1[path1.Length - 1];
if (chr == Path.DirectorySeparatorChar || chr == Path.AltDirectorySeparatorChar || chr == Path.VolumeSeparatorChar)
{
return string.Concat(path1, path2);
}
return string.Concat(path1, "\\", path2);
}
对Path做一个练习:
//目录分隔符 \
char directorySeparatorChar = Path.DirectorySeparatorChar;
//提供平台特定的备用目录分隔符 /
char altDirectorySeparatorChar = Path.AltDirectorySeparatorChar;
//提供平台特定的卷分隔符字符 :
char volumeSeparatorChar = Path.VolumeSeparatorChar;
//用于分隔环境变量中的路径字符串的平台特定分隔符 ;
char pathSeparator = Path.PathSeparator;
//无效的字符
char[] invalidPathChars = Path.InvalidPathChars;
// true
bool isPathRooted = Path.IsPathRooted(@"C:\Temp");
// false
bool hasExtension = Path.HasExtension(@"C:\Temp");
// true
bool hasExtension2 = Path.HasExtension(@"C:\Temp\aaa.txt");
// C:\Temp
string dirName = Path.GetDirectoryName(@"C:\Temp\aaa.txt");
// .txt
string extension = Path.GetExtension(@"C:\Temp\aaa.txt");
// aaa.txt
string fileName = Path.GetFileName(@"C:\Temp\aaa.txt");
// aaa
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(@"C:\Temp\aaa.txt");
// D:\MySpace\FileSystemInfoDemo\FileSystemInfoDemo\FileSystemInfoDemo\bin\Debug\aaa
string fullPath = Path.GetFullPath(@"aaa");
char[] invalidPathChars2 = Path.GetInvalidFileNameChars();
char[] invalidPathChars3 = Path.GetInvalidPathChars();
// C:\
string rootStr = Path.GetPathRoot(@"C:\Temp\aaa.txt");
string randomFileName = Path.GetRandomFileName();
// C:\Users\nestor.liu\AppData\Local\Temp\tmpD33A.tmp
string tempFileName = Path.GetTempFileName();
// C:\Users\nestor.liu\AppData\Local\Temp\
string tempPath = Path.GetTempPath();
//replace传入的path的后缀,返回新的path,仅仅是字符串操作而已
string newPath = Path.ChangeExtension(@"C:\Temp\aaa.txt", "ext");
// C:\\Temp\\aaa.txt
string newPath2 = Path.Combine(@"C:\Temp\aaa.txt", @"C:\Temp\aaa.txt");
// C:\\Temp\\aaa.txt
string newPath3 = Path.Combine(@"C:\Temp\", @"aaa.txt");
// \\Temp\\aaa.txt
string newPath4 = Path.Combine(@"C:\Temp\", @"\Temp\aaa.txt");
// C:\\Temp\\Temp\\aaa.txt
string newPath5 = Path.Combine(@"C:\Temp\", @"Temp\aaa.txt");
-纸上得来终觉浅,绝知此事要躬行-