init
This commit is contained in:
93
MarketDataLib/Utility/UpdateManager.cs
Normal file
93
MarketDataLib/Utility/UpdateManager.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
|
||||
namespace MarketData.Utils
|
||||
{
|
||||
public class UpdateManager
|
||||
{
|
||||
private StreamWriter streamWriter=null;
|
||||
private FileStream fileStream=null;
|
||||
private Dictionary<String,String> entries=new Dictionary<String,String>();
|
||||
|
||||
public UpdateManager()
|
||||
{
|
||||
}
|
||||
public bool Prepare(String strPathFileName)
|
||||
{
|
||||
try
|
||||
{
|
||||
String currentWorkingDirectory=Directory.GetCurrentDirectory();
|
||||
if(!File.Exists(strPathFileName)||IsExpired(strPathFileName))
|
||||
{
|
||||
if(File.Exists(strPathFileName))File.Delete(strPathFileName);
|
||||
fileStream=new FileStream(strPathFileName,FileMode.Create);
|
||||
streamWriter=new StreamWriter(fileStream);
|
||||
Console.WriteLine(String.Format("Creating session file:{0}",strPathFileName));
|
||||
}
|
||||
else
|
||||
{
|
||||
FileStream fileStream=new FileStream(strPathFileName,FileMode.Open);
|
||||
StreamReader streamReader=new StreamReader(fileStream);
|
||||
String strLine=null;
|
||||
while(null!=(strLine=streamReader.ReadLine()))
|
||||
{
|
||||
if(!entries.ContainsKey(strLine))entries.Add(strLine,strLine);
|
||||
}
|
||||
Console.WriteLine(String.Format("Loaded {0} entries from session file:{1}",entries.Count,strPathFileName));
|
||||
streamReader.Close();
|
||||
streamReader.Dispose();
|
||||
fileStream.Close();
|
||||
fileStream.Dispose();
|
||||
fileStream=new FileStream(strPathFileName,FileMode.Append);
|
||||
streamWriter=new StreamWriter(fileStream);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
Console.WriteLine(String.Format("Exception:{0}",exception));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
private bool IsExpired(String strPathFileName)
|
||||
{
|
||||
try
|
||||
{
|
||||
DateGenerator dateGenerator=new DateGenerator();
|
||||
DateTime creationTime=File.GetCreationTime(strPathFileName);
|
||||
int daysElapsed=Math.Abs(dateGenerator.DaysBetweenActual(creationTime,DateTime.Now));
|
||||
if(daysElapsed>5)
|
||||
{
|
||||
Console.WriteLine(String.Format("{0} is expired. {1} days old.",strPathFileName,daysElapsed));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
Console.WriteLine(String.Format("Exception:{0}",exception));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
public List<String> Entries
|
||||
{
|
||||
get{return new List<String>(entries.Keys);}
|
||||
}
|
||||
public void Add(String entry)
|
||||
{
|
||||
if(null==streamWriter)return;
|
||||
streamWriter.WriteLine(entry);
|
||||
streamWriter.Flush();
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
if(null!=streamWriter){streamWriter.Close();streamWriter.Dispose();}
|
||||
if(null!=fileStream){fileStream.Close();fileStream.Dispose();}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user