300 lines
10 KiB
C#
300 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.IO;
|
|
using NLog;
|
|
|
|
namespace ETS_Data
|
|
{
|
|
/// <summary>
|
|
/// classe gestione files
|
|
/// </summary>
|
|
public class fileMover
|
|
{
|
|
#region Protected Fields
|
|
|
|
protected Logger lg = LogManager.GetCurrentClassLogger();
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Public Fields
|
|
|
|
/// <summary>
|
|
/// oggetto static per fare chiamate sul magazzino
|
|
/// </summary>
|
|
public static fileMover obj = new fileMover();
|
|
|
|
#endregion Public Fields
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// verifica esistenza directory, eventualmente crea e restituisce controllo DirectoryInfo
|
|
/// </summary>
|
|
/// <param name="dirPath"></param>
|
|
/// <returns></returns>
|
|
public static DirectoryInfo checkDir(string dirPath)
|
|
{
|
|
Logger lg = LogManager.GetCurrentClassLogger();
|
|
DirectoryInfo _di = null;
|
|
try
|
|
{
|
|
_di = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath(dirPath));
|
|
if (!_di.Exists)
|
|
{
|
|
_di.Create();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
lg.Error($"Errore in determinazione mapPath:{Environment.NewLine}{exc}");
|
|
}
|
|
return _di;
|
|
}
|
|
|
|
/// <summary>
|
|
/// restituisce l'elenco dei files in una data cartella
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <returns></returns>
|
|
public static FileInfo[] elencoFile(string path)
|
|
{
|
|
FileInfo[] answ;
|
|
try
|
|
{
|
|
DirectoryInfo di = checkDir(path);
|
|
answ = di.GetFiles();
|
|
}
|
|
catch
|
|
{
|
|
answ = null;
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// verifica possibilità di creazione file (se non c'è lo crea e lo distrugge)
|
|
/// </summary>
|
|
/// <param name="dirPath"></param>
|
|
/// <returns></returns>
|
|
public static bool fileCouldExist(string filePath)
|
|
{
|
|
bool answ = false;
|
|
Logger lg = LogManager.GetCurrentClassLogger();
|
|
FileInfo _fi = null;
|
|
try
|
|
{
|
|
_fi = new FileInfo(System.Web.HttpContext.Current.Server.MapPath(filePath));
|
|
if (_fi.Exists)
|
|
{
|
|
answ = true;
|
|
}
|
|
else
|
|
{
|
|
// creo
|
|
FileStream fs = _fi.Create();
|
|
fs.Close();
|
|
_fi.Delete();
|
|
answ = true;
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
lg.Error($"Errore in determinazione possibilità esistenza file:{Environment.NewLine}{exc}");
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// restituisce una tab di files dato l'elenco dei files
|
|
/// </summary>
|
|
/// <param name="_files"></param>
|
|
/// <returns></returns>
|
|
public static DS_utils.filesDataTable tabellaFiles(FileInfo[] _files)
|
|
{
|
|
DS_utils.filesDataTable _dsEF = new DS_utils.filesDataTable();
|
|
DS_utils.filesRow _riga;
|
|
foreach (FileInfo _fi in _files)
|
|
{
|
|
_riga = _dsEF.NewfilesRow();
|
|
_riga.dataCreaz = _fi.CreationTime;
|
|
_riga.dataMod = _fi.LastWriteTime;
|
|
_riga.Nome = _fi.Name;
|
|
_riga.size = _fi.Length / 1000;
|
|
_dsEF.AddfilesRow(_riga);
|
|
}
|
|
return _dsEF;
|
|
}
|
|
|
|
/// <summary>
|
|
/// restituisce una tab di files contenuti nel path da analizzare
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <returns></returns>
|
|
public static DS_utils.filesDataTable tabellaFiles(string path)
|
|
{
|
|
return tabellaFiles(elencoFile(path));
|
|
}
|
|
|
|
/// <summary>
|
|
/// elimina un file dato path e nomefile
|
|
/// </summary>
|
|
/// <param name="Path"></param>
|
|
/// <param name="Original_Nome"></param>
|
|
/// <returns></returns>
|
|
public bool eliminaFile(string Path, string Original_Nome)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
//elimino file!
|
|
string filePath = string.Format("{0}\\{1}", Path, Original_Nome);
|
|
FileInfo _fi = new FileInfo(System.Web.HttpContext.Current.Server.MapPath(filePath));
|
|
_fi.Delete();
|
|
lg.Info("Eliminazione file: {0}/{1}", Path, Original_Nome);
|
|
answ = true;
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// elimina un file dato path completo del file
|
|
/// </summary>
|
|
/// <param name="fileFullPath"></param>
|
|
/// <returns></returns>
|
|
public bool eliminaFile(string fileFullPath)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
//elimino file!
|
|
FileInfo _fi = new FileInfo(System.Web.HttpContext.Current.Server.MapPath(fileFullPath));
|
|
_fi.Delete();
|
|
lg.Info("Eliminazione file: {0}", fileFullPath);
|
|
answ = true;
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
|
|
#if false
|
|
/// <summary>
|
|
/// elimina i file dato path
|
|
/// </summary>
|
|
/// <param name="Path"></param>
|
|
/// <param name="Original_Nome"></param>
|
|
/// <returns></returns>
|
|
public bool svuotaDirTemp(string Path)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
// verifico che il path contenga "TempUploads"...
|
|
if (Path.IndexOf("TempUploads") >= 0)
|
|
{
|
|
DirectoryInfo _di = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath(Path));
|
|
if (_di.Exists)
|
|
{
|
|
// elimino directory (ricorsiva)
|
|
_di.Delete(true);
|
|
lg.Info("Eliminazione directory: {0}", Path);
|
|
}
|
|
// la (ri)creo
|
|
_di.Create();
|
|
lg.Info("Ricreata directory file: {0}", Path);
|
|
answ = true;
|
|
}
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
#endif
|
|
|
|
/// <summary>
|
|
/// sposta il file da From a To...
|
|
/// </summary>
|
|
/// <param name="_pathFrom"></param>
|
|
/// <param name="_pathTo"></param>
|
|
/// <param name="_nomeFile"></param>
|
|
/// <param name="deleteOrig">determina se fare DAVVERO MOVE (cancella file origine) o solo copy (niente cancellazione file origine)</param>
|
|
/// <returns></returns>
|
|
public bool muoviFile(string _pathFrom, string _pathTo, string _nomeFile, bool deleteOrig)
|
|
{
|
|
bool fatto = false;
|
|
// verifica directory
|
|
checkDir(_pathFrom);
|
|
checkDir(_pathTo);
|
|
string filePathFrom = string.Format("{0}/{1}", _pathFrom, _nomeFile).Replace("//", "/");
|
|
string filePathTo = string.Format("{0}/{1}", _pathTo, _nomeFile).Replace("//", "/");
|
|
FileInfo _fi = new FileInfo(System.Web.HttpContext.Current.Server.MapPath(filePathFrom));
|
|
try
|
|
{
|
|
_fi.CopyTo(System.Web.HttpContext.Current.Server.MapPath(filePathTo), true);
|
|
if (deleteOrig)
|
|
{
|
|
_fi.Delete();
|
|
}
|
|
fatto = true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
lg.Error($"Eccezione in spostamento file!{Environment.NewLine}{e}");
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// sposta il file da From a To...
|
|
/// </summary>
|
|
/// <param name="_pathFrom"></param>
|
|
/// <param name="_pathTo"></param>
|
|
/// <param name="_nomeFileFrom"></param>
|
|
/// <param name="_nomeFileTo"></param>
|
|
/// <param name="deleteOrig">determina se fare DAVVERO MOVE (cancella file origine) o solo copy (niente cancellazione file origine)</param>
|
|
/// <returns></returns>
|
|
public bool muoviFile(string _pathFrom, string _pathTo, string _nomeFileFrom, string _nomeFileTo, bool deleteOrig)
|
|
{
|
|
bool fatto = false;
|
|
// verifica directory
|
|
checkDir(_pathFrom);
|
|
checkDir(_pathTo);
|
|
string filePathFrom = string.Format("{0}/{1}", _pathFrom, _nomeFileFrom).Replace("//", "/");
|
|
string filePathTo = string.Format("{0}/{1}", _pathTo, _nomeFileTo).Replace("//", "/");
|
|
FileInfo _fi = new FileInfo(System.Web.HttpContext.Current.Server.MapPath(filePathFrom));
|
|
try
|
|
{
|
|
_fi.CopyTo(System.Web.HttpContext.Current.Server.MapPath(filePathTo), true);
|
|
if (deleteOrig)
|
|
{
|
|
_fi.Delete();
|
|
}
|
|
fatto = true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
lg.Error($"Eccezione in spostamento file!{Environment.NewLine}{e}");
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// sposta il file da From a To...
|
|
/// </summary>
|
|
/// <param name="_pathFrom"></param>
|
|
/// <param name="_pathTo"></param>
|
|
/// <param name="_nomeFile"></param>
|
|
/// <returns></returns>
|
|
public bool muoviFile(string _pathFrom, string _pathTo, string _nomeFile)
|
|
{
|
|
bool fatto = muoviFile(_pathFrom, _pathTo, _nomeFile, true);
|
|
return fatto;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |