using System; using System.Collections.Generic; using System.Text; using System.IO; namespace VEPROMS.CSLA.Library { public partial class DocumentInfo { public string DocumentTitle { get { if (_LibTitle == "") return string.Format("Document {0}", _DocID); return _LibTitle; } } } public class DSOFile : IDisposable { private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); #region Fields private bool _IsDisposed; private static string _TemporaryFolder = null; private DocumentInfo _MyDocument = null; private FileInfo _MyFile = null; private string _Extension = "DOC"; #endregion #region Properties public static string TemporaryFolder { get { if (_TemporaryFolder == null) { _TemporaryFolder = string.Format(@"C:\Documents and Settings\{0}\Local Settings\Temp", Environment.UserName); if (!Directory.Exists(TemporaryFolder)) Directory.CreateDirectory(TemporaryFolder); _TemporaryFolder += @"\VE-PROMS"; if (!Directory.Exists(TemporaryFolder)) Directory.CreateDirectory(TemporaryFolder); } return _TemporaryFolder; } } public DocumentInfo MyDocument { get { return _MyDocument; } set { TryDelete(); _MyDocument = value; CreateFile(); } } public FileInfo MyFile { get { return _MyFile; } } public string Extension { get { return _Extension; } set { _Extension = value; } } #endregion #region Private Methods private void TryDelete() { if (_MyDocument == null) return; if (_MyFile == null) return; if (_MyFile.Exists) { try { _MyFile.Delete(); } catch (IOException ex) { _MyLog.Error("TryDelete",ex); } finally { _MyFile = null; _MyDocument = null; } } } private void CreateFile() { if (_MyDocument == null) return; _MyFile = new FileInfo(string.Format(@"{0}\tmp_{1}." + Extension , TemporaryFolder, MyDocument.DocID)); FileStream fs = _MyFile.Create(); fs.Write(MyDocument.DocContent, 0, MyDocument.DocContent.Length); fs.Close(); _MyFile.CreationTime = _MyDocument.DTS; _MyFile.LastWriteTime = _MyDocument.DTS; } public void SaveFile() { // TODO: Add Try & Catch logic if (_MyDocument == null) return; Document doc = _MyDocument.Get(); FileStream fs = _MyFile.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite); Byte[] buf = new byte[_MyFile.Length]; fs.Read(buf,0,buf.Length); fs.Close(); doc.DocContent = buf; doc.UserID = Environment.UserName; doc.DTS = _MyFile.LastWriteTime; doc.Save(); } #endregion #region Constructors public DSOFile(DocumentInfo myDocument) { MyDocument = myDocument; } #endregion #region Destructor ~DSOFile() { Dispose(false); } public void Dispose() { Dispose(false); GC.SuppressFinalize(this); } protected void Dispose(bool disposing) { if (!_IsDisposed) { _IsDisposed = true; TryDelete(); } } #endregion } }