using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using NLog; namespace ETS_Data { /// /// classe gestione files /// public class fileMover { #region inizializzazione /// /// oggetto static per fare chiamate sul magazzino /// public static fileMover obj = new fileMover(); protected Logger lg = LogManager.GetCurrentClassLogger(); #endregion /// /// restituisce una tab di files dato l'elenco dei files /// /// /// 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; } /// /// restituisce una tab di files contenuti nel path da analizzare /// /// /// public static DS_utils.filesDataTable tabellaFiles(string path) { return tabellaFiles(elencoFile(path)); } /// /// restituisce l'elenco dei files in una data cartella /// /// /// public static FileInfo[] elencoFile(string path) { FileInfo[] answ; try { DirectoryInfo di = checkDir(path); answ = di.GetFiles(); } catch { answ = null; } return answ; } /// /// verifica esistenza directory, eventualmente crea e restituisce controllo DirectoryInfo /// /// /// 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.ErrorException("Errore in determinazione mapPath: {0}", exc); } return _di; }/// /// verifica possibilità di creazione file (se non c'è lo crea e lo distrugge) /// /// /// 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.ErrorException("Errore in determinazione possibilità esistenza file: {0}", exc); } return answ; } /// /// elimina un file dato path e nomefile /// /// /// /// 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; } /// /// elimina un file dato path completo del file /// /// /// 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 /// /// elimina i file dato path /// /// /// /// 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 /// /// sposta il file da From a To... /// /// /// /// /// determina se fare DAVVERO MOVE (cancella file origine) o solo copy (niente cancellazione file origine) /// 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.ErrorException("Eccezione in spostamento file!", e); } return fatto; } /// /// sposta il file da From a To... /// /// /// /// /// /// determina se fare DAVVERO MOVE (cancella file origine) o solo copy (niente cancellazione file origine) /// 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.ErrorException("Eccezione in spostamento file!", e); } return fatto; } /// /// sposta il file da From a To... /// /// /// /// /// public bool muoviFile(string _pathFrom, string _pathTo, string _nomeFile) { bool fatto = muoviFile(_pathFrom, _pathTo, _nomeFile, true); return fatto; } } }