【发布时间】:2021-07-24 16:07:29
【问题描述】:
public void AddFile()
{
Console.Write("What file to add? : ");
string fileName = Console.ReadLine();
pathString = Path.Combine(FolderName, MainFolder,fileName);
using StreamWriter file = new(pathString, append: true);
file.Write("hello world");
string source = Path.Combine(FolderName, MainFolder, fileName);
string dest = Path.Combine(FolderName, SyncFolder, fileName);
File.Copy(source,dest,true);
}
为什么代码的最后一行会在我的 file.Write("hello world") 之前(或者至少是这样)复制文件?
首次启动:主文件夹中的文件有“hello world”。同步文件夹中的文件为空
首次启动:主文件夹中的文件有“hello world”x2。 Sync文件夹中的文件只有一个“hello world”
我该如何解决?
【问题讨论】:
-
在进行复制之前刷新或关闭。还要为 IDisposable 模式使用 using 关键字。
标签: c#