使用目录、文件和路径类管理本地目录和文件
在本地存储数据涉及在本地文件系统上管理文件和目录。 这包括创建、删除、移动、读取、写入、复制和操作文件和目录。 .NET Framework 在 System.IO
命名空间中提供了一组丰富的类,以方便这些操作。 和Directory
File
Path
类对于在 .NET 应用程序中执行文件和目录作至关重要。 了解如何使用这些类对于管理应用程序中的本地数据存储至关重要。
了解 Path 类
Path
.NET 中的类用于处理文件和目录路径。 它提供检索和修改路径信息的方法。
下面是类中 Path
一些最常用的方法:
Combine
:该方法Combine
用于将多个字符串合并到单个路径中。GetDirectoryName
:该方法GetDirectoryName
用于从路径获取目录信息。GetFileName
:该方法GetFileName
用于从路径获取文件名和扩展名。GetFileNameWithoutExtension
:该方法GetFileNameWithoutExtension
用于获取没有扩展名的文件名。GetExtension
:该方法GetExtension
用于获取文件的扩展名。GetFullPath
:该方法GetFullPath
用于获取文件的完全限定路径。GetTempPath
:该方法GetTempPath
用于获取当前系统的临时文件夹的路径。GetTempFileName
:该方法GetTempFileName
用于创建临时文件并返回其路径。
下面是如何使用 Path
类合并路径和获取文件信息的示例:
using System;
using System.IO;
class Program
{
static void Main()
{
string directoryPath = @"C:\ExampleDirectory";
string fileName = "example.txt";
// Combine directory and file name to create a full path
string fullPath = Path.Combine(directoryPath, fileName);
Console.WriteLine("Full Path: " + fullPath);
// Get the file name without extension
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fullPath);
Console.WriteLine("File Name Without Extension: " + fileNameWithoutExtension);
// Get the file extension
string fileExtension = Path.GetExtension(fullPath);
Console.WriteLine("File Extension: " + fileExtension);
}
}
注释
Path 类方法是静态的,无需创建类的实例即可使用。
了解 Directory 类
Directory
.NET 中的类提供用于创建、删除、移动和枚举目录的方法。 这些方法可用于管理应用程序中的目录。
下面是类中 Directory
一些最常用的方法:
Exists
:该方法Exists
用于检查目录是否存在。CreateDirectory
:该方法CreateDirectory
用于创建新目录。GetCurrentDirectory
:该方法GetCurrentDirectory
用于获取当前工作目录。GetFiles
:该方法GetFiles
用于获取指定目录中的文件名数组。Delete
:该方法Delete
用于删除现有目录。Move
:该方法Move
用于移动或重命名目录。EnumerateDirectories
:该方法EnumerateDirectories
用于列出指定路径中的所有目录。EnumerateFiles
:该方法EnumerateFiles
用于列出指定路径中的所有文件。GetDirectories
:该方法GetDirectories
用于获取指定目录中的目录名称数组。GetParent
:该方法GetParent
用于获取指定路径的父目录。
以下示例演示如何使用该 Directory
类创建目录、检查是否存在并枚举其文件:
using System;
using System.IO;
class Program
{
static void Main()
{
string directoryPath = @"C:\ExampleDirectory";
// Create a new directory
Directory.CreateDirectory(directoryPath);
// Check if the directory exists
if (Directory.Exists(directoryPath))
{
Console.WriteLine("Directory exists.");
// Enumerate files in the directory
foreach (string file in Directory.EnumerateFiles(directoryPath))
{
Console.WriteLine(file);
}
}
else
{
Console.WriteLine("Directory does not exist.");
}
}
}
注释
枚举目录中的文件时,可能会遇到 UnauthorizedAccessException
错误。 若要处理这些错误,请先枚举目录,然后枚举文件。
了解 File 类
File
.NET 中的类提供了执行各种文件作的方法,例如读取、写入、复制和删除文件。
下面是类中 File
一些最常用的方法:
Exists
:该方法Exists
用于检查文件是否存在。Create
:该方法Create
用于创建新文件。Delete
:该方法Delete
用于删除文件。Copy
:该方法Copy
用于将文件复制到新位置。Move
:该方法Move
用于移动或重命名文件。ReadAllText
:该方法ReadAllText
用于从文件读取所有文本。WriteAllText
:该方法WriteAllText
用于将文本写入文件。AppendText
:该方法AppendText
用于向文件追加文本。ReadAllLines
:该方法ReadAllLines
用于将文件中的所有行读取到字符串数组中。WriteAllLines
:该方法WriteAllLines
用于将字符串数组写入文件。ReadAllBytes
:该方法ReadAllBytes
用于将文件中的所有字节读取到字节数组中。WriteAllBytes
:该方法WriteAllBytes
用于将字节数组写入文件。Open
:该方法Open
用于打开文件进行读取或写入。OpenRead
:该方法OpenRead
用于打开要读取的文件。OpenWrite
:该方法OpenWrite
用于打开文件进行写入。OpenText
:该方法OpenText
用于打开用于读取文本的文件。GetAttributes
:该方法GetAttributes
用于获取文件的属性。SetAttributes
:该方法SetAttributes
用于设置文件的属性。
以下示例演示如何使用 File
类创建文件、向其写入文本以及读回文本:
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = @"C:\ExampleFile.txt";
// Create a new file and write text to it
File.WriteAllText(filePath, "Hello, World!");
// Read the text from the file
string text = File.ReadAllText(filePath);
Console.WriteLine(text);
}
}
注释
该File
类还提供用于检查文件是否存在()和获取或设置文件属性(File.Exists
File.GetAttributes
、 File.SetAttributes
) 的方法。
结合使用 Directory、File 和 Path 类
应用程序通常需要结合文件和目录操作。 这些Directory
、File
和Path
类可以一起使用,来执行复杂的文件 I/O操作。
以下示例演示如何创建目录、在该目录中创建文件、将文本写入文件以及读回文本:
using System;
using System.IO;
class Program
{
static void Main()
{
string directoryPath = @"C:\ExampleDirectory";
string fileName = "example.txt";
string filePath = Path.Combine(directoryPath, fileName);
// Create a new directory
Directory.CreateDirectory(directoryPath);
// Create a new file and write text to it
File.WriteAllText(filePath, "Hello, World!");
// Read the text from the file
string text = File.ReadAllText(filePath);
Console.WriteLine(text);
}
}
摘要
在本单元中,你学习了如何使用 .NET 中的 Directory
、File
和 Path
类来管理本地目录和文件。 你探索了这些类提供的方法,用于创建、删除、移动、读取、写入和操作文件和目录。 了解这些类对于在应用程序中进行有效的文件 I/O 操作至关重要。