指定のディレクトリのファイルの一覧を取得し、ファイルのパスをキーとしてデータベースのテーブルからコメント項目の文字列を取得するコードに成ります。
// ファイル・ディレクトリの一覧を取得する。
using System.IO;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
// dotnet new console
// dotnet add package Microsoft.EntityFrameworkCore
// dotnet add package Microsoft.EntityFrameworkCore.Sqlite.Core
// dotnet add package SQLitePCLRaw.bundle_green
// FileComment <= エントリ名
// FIleComments <= テーブル名
public class FileComment
{
[Key]
public string FullPath { get; set; } = "";
public string Comment { get; set; } = "";
}
public class AppDbContext : DbContext
{
public DbSet<FileComment> FileComments => Set<FileComment>();
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) =>
optionsBuilder.UseSqlite($"Data Source=FileSystemManager.db");
}
public class FileCommentModel
{
public string FullPath { get; set; } = "";
public string Comment { get; set; } = "";
}
public class Program
{
public static async IAsyncEnumerable<FileCommentModel> GetFileSystemInfos(string dir)
{
var paths = Directory.GetFileSystemEntries(dir);
using (var context = new AppDbContext())
{
var existingComments = await context.FileComments
.Where(fc => paths.Contains(fc.FullPath))
.ToListAsync();
foreach (var path in paths)
{
var commentEntity = existingComments.FirstOrDefault(ec => ec.FullPath == path);
yield return new FileCommentModel()
{
FullPath = path,
Comment = commentEntity?.Comment ?? ""
};
}
}
}
public static async Task Main()
{
// データベースの初期設定
using (var context = new AppDbContext())
{
context.Database.EnsureCreated(); // データベース作成
// UPSERT
var entity = new FileComment()
{
FullPath = @"H:\csharp\console\FileSystemEntries01\TEST_DATA\sample1.txt",
Comment = "コメント1",
};
var existingEntity = await context.FileComments.SingleOrDefaultAsync(x => x.FullPath == entity.FullPath); // 主キーで検索
if (existingEntity is null)
{
// 存在しないINSERT
context.FileComments.Add(entity);
}
else
{
// 既存UPDATE
context.Entry(existingEntity).CurrentValues.SetValues(entity);
}
await context.SaveChangesAsync();
}
string dir = @"H:\csharp\console\FileSystemEntries01\TEST_DATA";
await foreach(var info in GetFileSystemInfos(dir))
{
Console.WriteLine($"{info.FullPath} {info.Comment}");
}
}
}
ファイルマネージャーを作成していて、カレントディレクトリからファイルの一覧を取得するコードの試作に成ります。
なるべく非同期処理になるように試行錯誤しています。
コメント