如何写入JSON文件中的新行?C#

如何写入JSON文件中的新行?C#,c#,json,serialization,json.net,discord,C#,Json,Serialization,Json.net,Discord,因此,我目前正在创建一个带有清除命令的Discord机器人。但是,我想将时间、作者和删除的邮件量作为日志写入文件 我正在使用Newtonsoft.Json来实现这一点,我的代码就是这样 using (StreamWriter file = File.CreateText(@"C:\Users\COCON\source\repos\DiscordAdmin\DiscordAdmin\Logs\Purge.json")) //create json file at this path.

因此,我目前正在创建一个带有清除命令的Discord机器人。但是,我想将时间、作者和删除的邮件量作为日志写入文件

我正在使用Newtonsoft.Json来实现这一点,我的代码就是这样

using (StreamWriter file = File.CreateText(@"C:\Users\COCON\source\repos\DiscordAdmin\DiscordAdmin\Logs\Purge.json")) //create json file at this path.
        {
            JsonSerializer serializer = new JsonSerializer();
            //serialize object directly into file stream
            serializer.Serialize(file, LocalTime + ": " + Sender + " has executed the purge command on " + message + " messages"); //Serialize the time, the author of the command and how many messages they purged
        }

这段代码的问题是,每当它试图记录给定的命令时,它只会写入第一个日志。那么,我如何强制它每次都写一行新字呢?我更愿意继续使用这种方法。

您每次都在创建。您可以每次在文件名后面附加一个不同的文件,或者使用System.IO.file.writealText(filePath,jsonData);要仅写入已存在的文件,请编辑: 我先前的答复(下文)提到了上述问题。但正如在评论中指出的,它不会生成有效的json文件。仅在文件的每一行中使用有效的json。以下内容将生成包含多个条目的有效json文件:

List<object> log = new List<object>();
            JsonSerializer serializer = new JsonSerializer();
        string path = @"C:\Users\COCON\source\repos\DiscordAdmin\DiscordAdmin\Logs\Purge.json";

        if (System.IO.File.Exists(path))
        {
            using (System.IO.StreamReader reader = new System.IO.StreamReader(path))
            {
                Newtonsoft.Json.JsonReader jreader = new Newtonsoft.Json.JsonTextReader(reader);
                log = serializer.Deserialize<List<object>>(jreader);
            }
        }

        using (System.IO.StreamWriter file =
        new System.IO.StreamWriter(path, false))
        {
            object logEntry = LocalTime + ": " + Sender + " has executed the purge command on " + message + " messages";
            log.Add(logEntry);

            serializer.Serialize(file, log); //Serialize the time, the author of the command and how many messages they purged
        }
true”值表示追加,而不是创建新文件或覆盖。似乎CreateText没有该选项。 从文件.CreateText文档中:

此方法相当于StreamWriter(字符串, 布尔值) 将append参数设置为false的构造函数重载


文件。CreateText
将创建一个新文件并覆盖任何现有文件。请注意,如果执行此操作,您将无法神奇地反序列化文件,您必须在每次创建时逐行手动执行此操作。您可以每次在文件名后面附加一个不同的文件,或者使用System.IO.file.writealText(filePath,jsonData);只需写入已存在的文件exists@JoshAdams谢谢,更改了它,所以如果没有文件,它会创建一个文件,然后将其写入,如果有,它只会忽略创建一个文件,并使用新行直接写入!很高兴我能帮忙!你想让我发布一个解决方案还是你可以发布你自己的:)注意,这将产生无效的JSON(但你显然知道,因为这正是你想要的)…有效的JSON,每一个都在一行中。如果将整个文件作为单个json对象读取,则无效。如所问,“那么我如何强制它每次写入新行?”您的回答不清楚。。。(现在仍然不是…)显然OP不知道这一点,因为他们把接受支票转到了另一个我认为不太有用的答案。。。边注:虽然你的答案确实很好,但是已经有很多“如何附加到文件”的答案-所以,如果你想提供特殊的特殊情况处理,请考虑标记为重复的,或者至少引用现有的答案。我编辑了我的答案,以展示如何实现包含多个条目的有效json文件的一种可能解决方案。
using (System.IO.StreamWriter file =
        new System.IO.StreamWriter(@"C:\Users\COCON\source\repos\DiscordAdmin\DiscordAdmin\Logs\Purge.json", true))
        {
            JsonSerializer serializer = new JsonSerializer();
            //serialize object directly into file stream
            serializer.Serialize(file, LocalTime + ": " + Sender + " has executed the purge command on " + message + " messages"); //Serialize the time, the author of the command and how many messages they purged
        }