- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我现在收到 3 份用户机器在使用我的软件时崩溃的报告。崩溃与我的程序无关,但当他们重新启动配置文件时,我的程序写入的所有文件都已损坏。
文件的写入方式没有什么特别之处,只需创建一个 Json 表示并使用 File.WriteAllText() 将其转储到磁盘
// save our contents to the disk
string json = JsonConvert.SerializeObject(objectInfo, Formatting.Indented);
// write the contents
File.WriteAllText(path, json);
我有一位用户向我发送了其中一个文件,长度看起来是正确的 (~3kb),但内容都是 0x00。
根据下面的帖子,File.WriteAllText 应该关闭文件句柄,将所有未写入的内容刷新到磁盘:
In my C# code does the computer wait until output is complete before moving on?
但是,正如 Alberto 在评论中指出的那样:
System.IO.File.WriteAllText when completes, will flush all the text to the filesystem cache, then, it will be lazily written to the drive.
所以我假设这里发生的是文件正在被清除并用 0x00 初始化,但是当系统崩溃时数据还没有写入。
我正在考虑使用某种临时文件,所以过程是这样的:
我认为这不会解决问题,因为我认为即使 IO 仍未决,Windows 也会移动文件。
有什么方法可以强制机器将该数据转储到磁盘,而不是由它决定何时执行此操作或更新文件的更好方法?
更新:
根据@usr、@mikez 和@llya luzyanin 的建议,我创建了一个新的 WriteAllText 函数,它使用以下逻辑执行写入:
按照这个逻辑,如果最终文件加载失败,我的代码会检查备份文件并加载它
代码如下:
public static void WriteAllTextWithBackup(string path, string contents)
{
// generate a temp filename
var tempPath = Path.GetTempFileName();
// create the backup name
var backup = path + ".backup";
// delete any existing backups
if (File.Exists(backup))
File.Delete(backup);
// get the bytes
var data = Encoding.UTF8.GetBytes(contents);
// write the data to a temp file
using (var tempFile = File.Create(tempPath, 4096, FileOptions.WriteThrough))
tempFile.Write(data, 0, data.Length);
// replace the contents
File.Replace(tempPath, path, backup);
}
最佳答案
您可以使用 FileStream.Flush
将数据强制写入磁盘。写入临时文件并使用 File.Replace
自动替换目标文件。
我相信这一定会奏效。文件系统提供弱保证。这些保证几乎没有记录,而且很复杂。
或者,如果可用,您可以使用事务性 NTFS。它可用于 .NET。
FileOptions.WriteThrough
可以替代 Flush
,但如果您的数据超过单个簇的大小,您仍然需要临时文件。
关于c# - File.WriteAllText 不将数据刷新到磁盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25366534/
File.WriteAllText 在每个字母和引号后插入一个空格。 例子: 原始文件 "JobID" "ParentJobID" 新建文件 " J o b I D " " P a r e n
我正在尝试将许多不同的表单保存到一个文本文件中,目前我有: 温度 风 下雨 云 我遇到的问题是它只保存“云”数据。有没有一种方法可以保存提交时的所有信息,并用逗号、破折号或空格将它们分隔开? 这是我目
我现在收到 3 份用户机器在使用我的软件时崩溃的报告。崩溃与我的程序无关,但当他们重新启动配置文件时,我的程序写入的所有文件都已损坏。 文件的写入方式没有什么特别之处,只需创建一个 Json 表示并使
假设我正在使用 File.WriteAllText 将一个很长的字符串写入一个文件,而另一个线程或进程正在尝试读取同一个文件。它会抛出任何异常吗?换句话说,File.WriteAllText 方法使用
这段代码的输出 string fileDateTime = "StepsGA-" + DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss") + ".txt"; st
这个问题已经有答案了: File.WriteAllText not flushing data to disk (1 个回答) 已关闭 5 年前。 我有以下代码来保存数据: File.Copy("da
我有一个问题显然是 been encountered before ,尽管解决方案对我所看到的没有帮助。 我正在尝试根据 Dictionary 的内容的 base 64 编码将数据写入文件.它应该是这
我不明白为什么会这样,但确实如此。我正在写一个这样的 csv 文件: foreach (var row in record.rows) { csv += "\n"; csv += ro
我正在构建一个必须将用户输入写入文件的简单 C# 应用程序。 我的第一个问题是我希望用户能够设置设置文件的路径,但我希望程序能够在以下启动时读取设置文件。 我的第二个问题是,如果我使用类似的东西:
我有很多文件,我想随机写入一个文件,我想做的是: StringBuilder sb = new Stringbuilder(); int x; Random rnd = new Random();
对远程路径执行 File.WriteAllText 会抛出 UnauthorizedAccessException。当我在记事本中打开文件时,我可以毫无问题地对其进行编辑。试图修改文件的进程以我自己的
我有一些代码可以将字符串保存到文件中,如下所示: string TheFileJS = HttpRuntime.AppDomainAppPath + "\\SomePath\\" + ClientFi
文档说: // Summary: // Creates a new file, writes the specified string to the file, and then closes
这个问题在这里已经有了答案: File being used by another process after using File.Create() (11 个答案) 关闭 9 年前。 当我编写如
这一定是我见过的最奇怪的异常之一。使用File.WriteAllText静态方法写入文件时,不能写入特定文件名:“PRN”。你可以用一个简单的衬垫重新创建它: File.WriteAllText("c
我有一个非常简单的 C# 程序,它迭代多个文件并替换所有文件中的字符串。 但是,当我使用 Git 比较这些文件时,它突出显示了我所有文件的更改。 我的 C# 代码是: string[] files =
我注意到我使用以下代码创建的文件中没有换行符。在我还存储文本的数据库中,这些都存在。 string story = "Critical error occurred after " + elaps
我正在使用 File.WriteAllText 创建一个文件,并使用 File.Copy 方法将同一文件复制到另一个目录。但出于某种原因,它没有在源目录中创建文件,而是将其复制到目标目录。 可能是什么
我有一些原始数据 (xml),我收到的肯定包含 unicode。我使用以下方法将它们写入文件: File.WriteAllText 这似乎删除/更改了 unicode 字符。有办法避免这种情况吗? 最
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
我是一名优秀的程序员,十分优秀!