|
using System;
using System.IO;
using IpWatchDog.Log;
namespace IpWatchDog
{
class IpPersistor : IIpPersistor
{
private string _path;
private ILog _log;
public IpPersistor(ILog log)
{
_log = log;
var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var folder = Path.Combine(appData, "IpWatchDog");
Directory.CreateDirectory(folder);
_path = Path.Combine(folder, "currentIp.txt");
}
public string GetIp()
{
if (!File.Exists(_path)) return null;
try
{
using (var reader = new StreamReader(_path))
{
return reader.ReadToEnd().Trim();
}
}
catch (Exception ex)
{
_log.Write(LogLevel.Warning, "Error reading IP from storage. {0}", ex);
return null;
}
}
public void SaveIp(string ip)
{
try
{
using (var writer = new StreamWriter(_path))
{
writer.Write(ip);
}
}
catch (Exception ex)
{
_log.Write(LogLevel.Warning, "Error writing IP to storage. {0}", ex);
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Ivan is a hands-on software architect/technical lead working for Thomson Reuters in the New York City area. At present I am mostly building complex multi-threaded WPF application for the financial sector, but I am also interested in cloud computing, web development, mobile development, etc.
Please visit my web site:
www.ikriv.com.