类 | 功能 | 函数 | 举例 |
File | 用于创建、复制、移动、删除和读写文件等操作 | ||
File | 创建文件 | Create | string filePath = “C:\path\to\file.txt“; File.Create(filePath); |
复制文件 | Copy | string sourceFilePath = “C:\path\to\source.txt“; string destFilePath = “C:\path\to\destination.txt“; File.Copy(sourceFilePath, destFilePath); |
|
移动文件 | Move | string sourceFilePath = “C:\path\to\source.txt“; string destFilePath = “C:\path\to\destination.txt“; File.Move(sourceFilePath, destFilePath); |
|
删除文件 | Delete | string filePath = “C:\path\to\file.txt”; File.Delete(filePath); |
|
读取文件内容 | ReadAllText | string filePath = “C:\path\to\file.txt”; string content = File.ReadAllText(filePath); |
|
写入文件内容 | WriteAllText | string filePath = “C:\path\to\file.txt”; string content = “Hello, World!”; File.WriteAllText(filePath, content); |
|
Directory | Directory类:用于创建、复制、移动和删除文件夹等操作 | ||
Directory |
创建 | CreateDirectory | string path = “C:\path\to\folder“; Directory.CreateDirectory(path); |
删除 | Delete | string path = “C:\path\to\folder“; Directory.Delete(path); |
|
移动 | Move | string sourcePath = “C:\path\to\folder“; string destinationPath = “C:\new\path\to\folder”; Directory.Move(sourcePath, destinationPath); |
|
检查 | Exists | string path = “C:\path\to\folder”; bool exists = Directory.Exists(path); |
|
获取子文件夹 | GetDirectories | string path = “C:\parent\folder”; string[] subDirectories = Directory.GetDirectories(path); foreach (string subDirectory in subDirectories) { Console.WriteLine(subDirectory); } |
|
获取子文件 | GetFiles | string path = “C:\parent\folder”; string[] files = Directory.GetFiles(path); foreach (string file in files) { Console.WriteLine(file); } |
|
拷贝 | Copy | string sourcePath = “C:\source\folder”; string destinationPath = “C:\destination\folder”; Directory.Copy(sourcePath, destinationPath); |
|
Path |
Path类是System.IO命名空间中用于处理文件和文件夹路径的常见操作的类。它提供了一组静态方法,可以轻松处理路径字符串。以下是一些Path类常用的方法 | ||
Path | 组合 | Combine | string path1 = “C:\path\to”; string path2 = “file.txt”; string combinedPath = Path.Combine(path1, path2); // 结果:C:pathtofile.txt |
获取文件名 | GetFileName | string path = “C:\path\to\file.txt”; string fileName = Path.GetFileName(path); // 结果:file.txt |
|
获取路径 | GetDirectoryName | string path = “C:\path\to\file.txt”; string directoryName = Path.GetDirectoryName(path); // 结果:C:pathto |
|
获取扩展名 | GetExtension | string path = “C:\path\to\file.txt”; string extension = Path.GetExtension(path); // 结果:.txt |
|
获取文件名 不含扩展名
|
GetFileName |
string path = “C:\path\to\file.txt”; string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path); // 结果:file |
|
绝对路径 | GetFullPath | string relativePath = “folder\file.txt”; string fullPath = Path.GetFullPath(relativePath); // 结果:C:currentdirectoryfolderfile.txt |
原文地址:https://blog.csdn.net/xie__jin__cheng/article/details/134693752
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_49789.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。