Files
2026-06-01 12:28:44 +09:00

141 lines
5.1 KiB
C#

using DocumentFormat.OpenXml.Vml;
using KssSmaPlaLib.Commons;
using KssSmaPlaLib.Exchange.Interfaces;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Path = System.IO.Path;
namespace KssSmaPlaLib.IO.File
{
public static class FileUxer
{
/// <summary>
/// テキストファイル出力(全テキスト一括)
/// </summary>
/// <param name="allLineText">全テキスト</param>
/// <param name="filePath">ファイルパス</param>
/// <param name="resultMessage">メッセージ</param>
/// <param name="encodingType">文字コード</param>
/// <returns>成否</returns>
public static bool WriteAllText(string allLineText, string filePath, out string resultMessage, EncodingUxer.EncodingType encodingType = EncodingUxer.EncodingType.Utf8WithoutBom)
{
resultMessage = string.Empty;
try
{
System.IO.File.WriteAllText(filePath, allLineText, EncodingUxer.GetEncoding(encodingType));
return true;
}
catch (Exception ex)
{
resultMessage = $"CSV出力に失敗しました。[ファイルパス:{filePath},エラー内容:{ex.Message}]";
return false;
}
}
public static bool TryDeletePatternFiles(
string folderPath,
string fileNamePattern,
out int fileCount,
out string resultMessage)
{
fileCount = 0;
resultMessage = string.Empty;
if (!Directory.Exists(folderPath))
{
resultMessage = $"フォルダが存在しません。[フォルダパス:{folderPath}]";
return false;
}
try
{
var files = Directory.GetFiles(folderPath, fileNamePattern);
foreach (var filePath in files)
{
System.IO.File.Delete(filePath);
fileCount++;
}
return true;
}
catch (Exception ex)
{
resultMessage = $"ファイル削除中にエラーが発生しました。[フォルダパス:{folderPath},ファイル名パターン:{fileNamePattern},エラー内容:{ex.Message}]";
return false;
}
}
public static string GetTimestampedFileName(string originalName, DateTime dt, string prefixDtFormat, string suffixDtFormat)
{
string nameWithoutExt = Path.GetFileNameWithoutExtension(originalName);
string ext = Path.GetExtension(originalName);
string prefixDtStr = dt.ToString(prefixDtFormat);
string suffixDtStr = dt.ToString(suffixDtFormat);
string newName = prefixDtStr + nameWithoutExt + suffixDtStr;
return newName + ext;
}
// public static bool CheckParentFolderExists(string folderPath, out string parentPath)
// {
// parentPath = Path.GetDirectoryName(folderPath) ?? string.Empty;
// return Directory.Exists(parentPath);
// }
// public static bool TryApplyActionToFiles<T>(
// string folderPath,
// Func<FileInfo, bool> condition,
// Action<FileInfo> action,
// out int fileCount,
// out string resultMessage,
// string actionName = "操作",
// bool isDescending = true,
// Func<FileInfo, T>? sortKey = null,
// int skip = 0)
// {
// fileCount = 0;
// resultMessage = string.Empty;
// try
// {
// // 条件未指定時は全件対象
// condition = condition ?? (_ => true);
// // 一旦条件で取得
// var files = new DirectoryInfo(folderPath)
// .GetFiles()
// .Where(condition);
// // ソートキー作成
// var keySelector =
// sortKey != null ? (Func<FileInfo, object>)
// (f => sortKey(f)) : f => f.LastWriteTime;
// // ソート+スキップ+リスト化
// var filesList =
// (isDescending ?
// files.OrderByDescending(keySelector) :
// files.OrderBy(keySelector))
// .Skip(skip)
// .ToList();
// foreach (var file in filesList)
// {
// action(file);
// LoggerUxer.Info($"{actionName}しました。[{file}]");
// }
// fileCount = filesList.Count;
// return true;
// }
// catch (Exception ex)
// {
// resultMessage = $"ファイル群の{actionName}に失敗しました。[エラー内容:{ex.Message}]";
// return false;
// }
// }
}
}