EBS_POC/EBS POC/Utilities/FilePlus.cs

220 lines
6.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Threading.Tasks;
public static class FilePlus
{
public static async Task WriteAllText(string FilePath, string Contents)
{
var count = 0;
var success = false;
while (count < 20 && success == false)
{
if (count != 0)
{
await Task.Delay(1000);
}
try
{
var dir = Path.GetDirectoryName(FilePath);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
File.WriteAllText(FilePath, Contents);
success = true;
}
catch
{
count++;
}
}
if (!success)
{
File.WriteAllText(FilePath + " Write Failure " + DateTime.Now.ToString().Replace('/', '-').Replace(':', '.'), Contents);
}
}
public static async Task WriteAllLines(string FilePath, string[] Contents)
{
var count = 0;
var success = false;
while (count < 20 && success == false)
{
if (count != 0)
{
await Task.Delay(1000);
}
try
{
var dir = Path.GetDirectoryName(FilePath);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
var strContents = "";
foreach (var line in Contents)
{
strContents += line + Environment.NewLine;
}
File.WriteAllText(FilePath, strContents);
success = true;
}
catch
{
count++;
}
}
if (!success)
{
var strContents = "";
foreach (var line in Contents)
{
strContents += line + Environment.NewLine;
}
File.WriteAllText(FilePath + " Write Failure " + DateTime.Now.ToString().Replace('/', '-').Replace(':', '.'), strContents);
}
}
public static async Task AppendAllText(string FilePath, string Contents)
{
var count = 0;
var success = false;
while (count < 20 && success == false)
{
if (count != 0)
{
await Task.Delay(1000);
}
try
{
var dir = Path.GetDirectoryName(FilePath);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
File.AppendAllText(FilePath, Contents);
success = true;
}
catch
{
count++;
}
}
if (!success)
{
File.WriteAllText(FilePath + " Write Failure " + DateTime.Now.ToString().Replace('/', '-').Replace(':', '.'), Contents);
}
}
public static async Task AppendAllLines(string FilePath, string[] Contents)
{
var count = 0;
var success = false;
while (count < 20 && success == false)
{
if (count != 0)
{
await Task.Delay(1000);
}
try
{
var dir = Path.GetDirectoryName(FilePath);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
foreach (var line in Contents)
{
File.AppendAllText(FilePath, line + Environment.NewLine);
}
success = true;
}
catch
{
count++;
}
}
if (!success)
{
var strContents = "";
foreach (var line in Contents)
{
strContents += line + Environment.NewLine;
}
File.WriteAllText(FilePath + " Write Failure " + DateTime.Now.ToString().Replace('/', '-').Replace(':', '.'), strContents);
}
}
public static async Task<string> ReadAllText(string FilePath)
{
var count = 0;
var success = false;
var contents = "";
while (count < 20 && success == false)
{
if (count != 0)
{
await Task.Delay(1000);
}
try
{
var dir = Path.GetDirectoryName(FilePath);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
if (!File.Exists(FilePath))
{
File.Create(FilePath).Close();
}
contents = File.ReadAllText(FilePath);
success = true;
}
catch
{
count++;
}
}
if (!success)
{
File.WriteAllText(FilePath + " Read Failure " + DateTime.Now.ToString().Replace('/', '-').Replace(':', '.'), "");
}
return contents;
}
public static async Task<string[]> ReadAllLines(string FilePath)
{
var count = 0;
var success = false;
var contents = new string[0];
while (count < 20 && success == false)
{
if (count != 0)
{
await Task.Delay(1000);
}
try
{
var dir = Path.GetDirectoryName(FilePath);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
if (!File.Exists(FilePath))
{
File.Create(FilePath).Close();
}
contents = File.ReadAllLines(FilePath);
success = true;
}
catch
{
count++;
}
}
if (!success)
{
File.WriteAllText(FilePath + " Read Failure " + DateTime.Now.ToString().Replace('/', '-').Replace(':', '.'), "");
}
return contents;
}
}