在c#中将文件列表转储到JSON文件中

本文关键字:文件 JSON 转储 列表 | 更新日期: 2023-09-27 18:05:28

我正在学习写JSON,我已经这样做了:

        try
        {
            string path = @"C:'";
            string[] dirs = Directory.GetFiles(path, "*.exe");
            using (StreamWriter file = File.CreateText(@"filelist.json"))
                foreach (string dir in dirs)
                {
                    FolderFiles aFile = new FolderFiles
                    {
                        FileName = dir.ToString(),
                        FileType = Path.GetExtension(path + @"'" + dir.ToString())
                    };
                    JsonSerializer serializer = new JsonSerializer();
                    serializer.Serialize(file, aFile);
                }
            MessageBox.Show("Process Done");
        }

我试图在JSON文件中转储文件名列表,但是当我验证它时,它说它是无效的,而且当我打开它时,它只在一行上。

JSON可以按顺序写入文件吗?任何帮助将是伟大的,谢谢

当前输出:

{"FileName":"C:''Users''Joko''Documents''Visual Studio 2015''Projects''Research''GetFilesFromFolder''GetFilesFromFolder''bin''Debug''GetFilesFromFolder.exe","FileType":".exe"}{"FileName":"C:''Users''Joko''Documents''Visual Studio 2015''Projects''Research''GetFilesFromFolder''GetFilesFromFolder''bin''Debug''GetFilesFromFolder.vshost.exe","FileType":".exe"}

这是文件夹文件:

internal class FolderFiles {
    public string FileName { get; set; }
    public string FileType { get; set; }
}

在c#中将文件列表转储到JSON文件中

在。net中不可能使用开箱即用的工具顺序写入JSON文件。你必须自己实现它,手动地一点一点地写入文件。

相反,只需构建包含文件夹路径数组的对象,将其序列化为JSON,然后将其一次性写入文件。

实现该功能的示例代码,使用您作为示例输出提供的硬编码数据:

public class FolderFilesAggregate
{
    public IEnumerable<FolderFiles> FolderFiles { get; set; }
}
public class FolderFiles
{
    public string FileName { get; set; }
    public string FileType { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        // get your folder files in any way you want to, I hardcoded it to simplify the example
        var folderFiles = new List<FolderFiles>
                          {
                              new FolderFiles
                              {
                                  FileName = @"C:''Users''Joko''Documents''Visual Studio 2015''Projects''Research''GetFilesFromFolder''GetFilesFromFolder''bin''Debug''GetFilesFromFolder.exe",
                                  FileType = ".exe"
                              },
                              new FolderFiles
                              {
                                  FileName = @"C:''Users''Joko''Documents''Visual Studio 2015''Projects''Research''GetFilesFromFolder''GetFilesFromFolder''bin''Debug''GetFilesFromFolder.vshost.exe",
                                  FileType = ".exe"
                              }
                          };
        var folderFilesAggregate = new FolderFilesAggregate
                                   {
                                       FolderFiles = folderFiles
                                   };
        // serialize your aggregate object
        var serializedFolderFilesAggregate = Newtonsoft.Json.JsonConvert.SerializeObject(folderFilesAggregate, Formatting.Indented);
        // write it to a file
        System.IO.File.WriteAllText(@"C:'output.json", serializedFolderFilesAggregate);
    }
}