C# 文件操作(复制、移动、重命名、创建、打开、删除)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | 目录 一、简介 二、创建文件 三、写入文件 四、读取文件 五、复制文件 六、移动文件 七、重命名文件 八、删除文件 结束 一、简介 C#中的IO(Input/Output)操作包括读取和写入文件、读取和写入流、以及操作目录和文件夹等。这些操作都可以通过System.IO命名空间中的类实现。下面对C# IO相关的类和操作进行介绍。 文件操作 File类 File类提供了对文件的创建、读取、写入、复制、移动、重命名和删除等操作。 StreamReader和StreamWriter类 StreamReader和StreamWriter类用于读取和写入文本文件。 目录和文件夹操作 Directory和DirectoryInfo类 Directory和DirectoryInfo类提供了对目录和文件夹的创建、移动、重命名和删除等操作。 二、创建文件 要创建一个文件,可以使用System.IO.File.Create()方法。该方法接受一个文件路径和一个可选的缓冲区大小作为参数,然后返回一个FileStream对象,该对象可用于写入文件。 代码: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 文件操作 { internal class Program { static void Main( string [] args) { try { string filePath = @"E:\myFile.txt" ; FileStream fileStream = File.Create(filePath); fileStream.Close(); } catch (Exception ex) { Console.WriteLine( "创建文件失败:" + ex.Message); } Console.WriteLine( "创建文件成功!" ); Console.ReadKey(); } } } 打开E盘,文件是创建成功了,只是这个 txt 文件里没有任何内容 三、写入文件 写入文件可以使用下面方式: 1. File.WriteAllText(FilePath,String) 2. File.WriteAllText(FilePath, String,Encoding) ----指定编码 3. File.WriteAllLines(FilePath,String[]) 4. File.WriteAllLines(FilePath,String[],Encoding) ----指定编码 前面两种写入的是一个字符串,后面两种写入的是一个字符串数组。 使用 File.WriteAllText 或 File.WriteAllLines 方法时,如果指定的文件路径不存在,会创建一个新文件;如果文件已经存在,则会覆盖原文件。 第一种:指定字符串的写入 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 文件操作 { internal class Program { static void Main( string [] args) { string filePath = @"E:\myFile.txt" ; string content = "读取读取读取读取读取读取" ; //第一种 //File.WriteAllText(filePath, content); //第二种 File.WriteAllText(filePath, content, Encoding.UTF8); Console.ReadKey(); } } } 打开刚创建的 myFile.txt 文件: 第二种:字符串数组的写入 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 文件操作 { internal class Program { static void Main( string [] args) { string filePath = @"E:\myFile.txt" ; string [] contentArr = { "dddd" , "eeeeee" }; //第一种 //File.WriteAllLines(filePath, contentArr); //第二种 File.WriteAllLines(filePath, contentArr, Encoding.UTF8); Console.ReadKey(); } } } 上面写入的字符串现在已经被覆盖了 四、读取文件 读取文件可以使用下面方式: 1.File.ReadAllText(FilePath) 2.File.ReadAllText(FilePath, Encoding) ----指定编码 3.File.ReadAllLines(FilePath) 4.File.ReadAllLines(FilePath, Encoding) ----指定编码 以字符串接收方式 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 文件操作 { internal class Program { static void Main( string [] args) { string path = @"E:\myFile.txt" ; //string content = File.ReadAllText(path); string content = File.ReadAllText(path,Encoding.UTF8); Console.WriteLine(content); Console.ReadKey(); } } } 以字符串数组接收的方式 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 文件操作 { internal class Program { static void Main( string [] args) { string path = @"E:\myFile.txt" ; //string[] content = File.ReadAllLines(path); string [] content = File.ReadAllLines(path, Encoding.UTF8); for ( int i = 0; i < content.Length; i++) { Console.WriteLine(content[i]); } Console.ReadKey(); } } } 采用流(Stream)的方式来读取内容 1.StreamReader(FilePath) 2.StreamReader(FilePath, Encoding) 3.StreamReader(FileStream) 4.StreamReader(FileStream, Encoding) 5.File.OpenText(FilePath) 6.FileInfo.OpenText() 基于StreamReader,一行一行读取 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 文件操作 { internal class Program { static void Main( string [] args) { string path = @"E:\myFile.txt" ; //StreamReader sr1 = new StreamReader(path); //StreamReader sr2 = new StreamReader(path, Encoding.UTF8); //初始化FileStream FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None); //StreamReader sr3 = new StreamReader(fs); StreamReader sr4 = new StreamReader(fs, Encoding.UTF8); string line = string .Empty; while ((line = sr4.ReadLine()) != null ) { Console.WriteLine(line); } sr4.Close(); Console.ReadKey(); } } } 一次性读完 string path = @"E:\myFile.txt" ; StreamReader sr = new StreamReader(path, Encoding.UTF8); string content = sr.ReadToEnd(); Console.WriteLine(content); sr.Close(); 五、复制文件 要复制文件,可以使用System.IO.File.Copy()方法。该方法接受源文件路径和目标文件路径作为参数,并将源文件复制到目标文件。如果目标文件已存在,则将被覆盖。 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 文件操作 { internal class Program { static void Main( string [] args) { string sourceFilePath = @"E:\myFile.txt" ; string destinationFilePath = @"E:\myFile_copy.txt" ; File.Copy(sourceFilePath, destinationFilePath); Console.ReadKey(); } } } 打开对应的目录,可以看到拷贝成功了,而且字节数也是一样的 六、移动文件 要移动文件,可以使用System.IO.File.Move()方法。该方法接受源文件路径和目标文件路径作为参数,并将源文件移动到目标文件。如果目标文件已存在,则将被覆盖。 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 文件操作 { internal class Program { static void Main( string [] args) { string sourceFilePath = @"E:\myFile.txt" ; string destinationFilePath = @"D:\myFile.txt" ; File.Move(sourceFilePath, destinationFilePath); Console.ReadKey(); } } } 七、重命名文件 要重命名文件,可以使用System.IO.File.Move()方法。该方法接受源文件路径和目标文件路径作为参数,并将源文件重命名为目标文件。如果目标文件已存在,则将被覆盖。 在上一节 移动文件 中,我们将 myFile.txt 文件移动到 D 盘了,执行下面操作之前,要先剪切回来再执行操作哦 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 文件操作 { internal class Program { static void Main( string [] args) { string filePath = @"E:\myFile.txt" ; string newFilePath = @"E:\myFile_new.txt" ; File.Move(filePath, newFilePath); Console.ReadKey(); } } } 运行后,可以看到效果了 八、删除文件 要删除文件,可以使用System.IO.File.Delete()方法。该方法接受文件路径作为参数,并将该文件从磁盘上删除。 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 文件操作 { internal class Program { static void Main( string [] args) { string newFilePath = @"E:\myFile_new.txt" ; File.Delete(newFilePath); Console.ReadKey(); } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
2017-03-08 Calling a Web API From a .NET Client (C#)