注意看string中,加的是\n还是\r\n? 两者有什么区别?在Linux、Windows、Mac系统中具体用哪个? 有空时,可以详细了解一下。
关于 File.AppendAllText 如何释放资源的问题,可以查看相关的源代码来做简单了解 网址:https://referencesource.microsoft.com/#mscorlib/system/io/file.cs,4ff1446b33bdfb6b 两个关键的代码段
[ResourceExposure(ResourceScope.Machine)] [ResourceConsumption(ResourceScope.Machine)] public static void AppendAllText(String path, String contents) { if (path == null) throw new ArgumentNullException("path"); if (path.Length == 0) throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath")); Contract.EndContractBlock(); InternalAppendAllText(path, contents, StreamWriter.UTF8NoBOM); } [ResourceExposure(ResourceScope.Machine)] [ResourceConsumption(ResourceScope.Machine)] private static void InternalAppendAllText(String path, String contents, Encoding encoding) { Contract.Requires(path != null); Contract.Requires(encoding != null); Contract.Requires(path.Length > 0); using (StreamWriter sw = new StreamWriter(path, true, encoding)) sw.Write(contents); }
感恩曾经帮助过 心少朴 的人。 C#优秀,值得学习。.NET Core具有跨平台的能力,值得关注。 Console,WinForm,WPF,ASP.NET,Azure WebJob,WCF,Unity3d,UWP可以适当地了解。 注:此文是自学笔记所生,质量中下等,故要三思而后行。新手到此,不可照搬,应先研究其理象数,待能变通之时,自然跳出深坑。
