зеркало из
1
0
Форкнуть 0

Merge pull request #386 from wangcai0124/VolumeDockerFileWindowsRS5

Handle the case that dataFile content is empty or non-number
This commit is contained in:
wangcai0124 2019-11-18 13:04:23 -08:00 коммит произвёл GitHub
Родитель 81814358fe 8a7f2eb7f8
Коммит caf39e7788
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 27 добавлений и 6 удалений

Просмотреть файл

@ -27,17 +27,38 @@ namespace AzureFilesVolumeTestApp
Directory.CreateDirectory(dataFolderFullPath); Directory.CreateDirectory(dataFolderFullPath);
var dataFileFullPath = Path.Combine(dataFolderFullPath, DataFileName); var dataFileFullPath = Path.Combine(dataFolderFullPath, DataFileName);
var sequenceNumber = 0;
string fileContent = "";
try
{
if (File.Exists(dataFileFullPath))
{
// The content may be empty or non-number, catch the exception to avoid crash
fileContent = File.ReadAllText(dataFileFullPath);
sequenceNumber = Int32.Parse(fileContent);
}
}
catch (Exception e)
{
Console.WriteLine($"Exception {e.ToString()}");
Console.WriteLine($"Invalid file content {fileContent}");
}
var sequenceNumber = File.Exists(dataFileFullPath) ? Int32.Parse(File.ReadAllText(dataFileFullPath)) : 0;
for(;;) for(;;)
{ {
sequenceNumber++; sequenceNumber++;
try
using (var file = new FileStream(dataFileFullPath, FileMode.OpenOrCreate, FileAccess.Write))
{ {
var bytes = Encoding.ASCII.GetBytes(sequenceNumber.ToString()); using (var file = new FileStream(dataFileFullPath, FileMode.OpenOrCreate, FileAccess.Write))
file.Write(bytes, 0, bytes.Length); {
file.Flush(); var bytes = Encoding.ASCII.GetBytes(sequenceNumber.ToString());
file.Write(bytes, 0, bytes.Length);
file.Flush();
}
}
catch (Exception e)
{
Console.WriteLine($"Exception {e.ToString()}");
} }
Thread.Sleep(PauseBetweenUpdatesMillisec); Thread.Sleep(PauseBetweenUpdatesMillisec);