44 lines
1.5 KiB
C#
44 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MP.FileData
|
|
{
|
|
public class FileUtils
|
|
{
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Elenco dei file data la directory da controllare
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <param name="searchPattern"></param>
|
|
/// <returns></returns>
|
|
public static FileInfo[] GetFileList(string path, string searchPattern)
|
|
{
|
|
//string[] answ = Directory.GetFiles(path, searchPattern);
|
|
DriveInfo di = new DriveInfo(path);
|
|
DirectoryInfo dirInfo = di.RootDirectory;
|
|
FileInfo[] answ = dirInfo.GetFiles("*.*");
|
|
|
|
return answ;
|
|
}
|
|
|
|
public static async Task SaveToCsv<T>(List<T> reportData, string path)
|
|
{
|
|
var lines = new List<string>();
|
|
IEnumerable<PropertyDescriptor> props = TypeDescriptor.GetProperties(typeof(T)).OfType<PropertyDescriptor>();
|
|
var header = string.Join(";", props.ToList().Select(x => x.Name));
|
|
lines.Add(header);
|
|
var valueLines = reportData.Select(row => string.Join(";", header.Split(';').Select(a => row.GetType().GetProperty(a).GetValue(row, null))));
|
|
lines.AddRange(valueLines);
|
|
await Task.Run(() => File.WriteAllLines(path, lines.ToArray()));
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |