Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public sealed class BankDownloader
- {
- private string _path;
- public bool Controller()
- {
- _path= Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"temp.txt");
- return (File.Exists(_path)) ? UpdateFile() : DownloadFile();
- }
- public bool DownloadFile()
- {
- using (var connector = new System.Net.WebClient())
- {
- try
- {
- var encodedPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),@"tempEncoded.txt");
- connector.DownloadFile(@"http://www.nbp.pl/banki_w_polsce/ewidencja/dz_bank_jorg.txt",encodedPath);
- using (var sw = new StreamWriter(_path))
- {
- sw.WriteLine($"{DateTime.Now}\n");
- sw.WriteLine(File.ReadAllText(encodedPath,Encoding.GetEncoding(852)));
- }
- File.Delete(encodedPath);
- return true;
- }
- catch { return false; }
- }
- }
- public bool UpdateFile()
- {
- if (DateTime.Parse(File.ReadAllLines(_path).First()) >= DateTime.Now.AddDays(-7)) return true;
- RemoveFile();
- return DownloadFile();
- }
- public void RemoveFile()
- {
- File.Delete(_path);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment