PEPTool/AV-ToolV3/Writer.cs
2019-07-26 07:36:50 +02:00

70 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PEP_Tool
{
public static class Writer
{
static int errCount = 0;
public static FileStream WaitForFile(string fullPath, FileMode mode, FileAccess access, FileShare share)
{
var err = false;
FileStream fs = null;
for (int numTries = 0; numTries < 50; numTries++)
{
fs = null;
try
{
if(err && errCount > 5) LogFile.WriteLine(access.ToString() + "error #" + errCount + ": " + Path.GetFileName(fullPath));
fs = new FileStream(fullPath, mode, access, share);
if (err && errCount > 5) LogFile.WriteLine(access.ToString() + " done" + ": " + Path.GetFileName(fullPath));
err = false;
errCount = 0;
return fs;
}
catch (IOException ex)
{
errCount++;
//LogFile.WriteLine($"{Environment.MachineName}: {access.ToString()} denied: {Path.GetFileName(fullPath)}\n\nDies ist das {errCount}. vorkommen\n\n");
//LogFile.WriteLine(ex.ToString());
//LogFile.WriteLine("Message: " + ex.Message);
//if(ex.InnerException != null) LogFile.WriteLine("InnerException: " + ex.InnerException);
err = true;
if (fs != null)
{
fs.Dispose();
}
System.Threading.Thread.Sleep(500);
}
catch(Exception exc)
{
//LogFile.WriteLine(Environment.MachineName + ": " + access.ToString() + " denied" + ": " + Path.GetFileName(fullPath));
//LogFile.WriteLine(exc.ToString());
//LogFile.WriteLine("Message: " + exc.Message);
//if (exc.InnerException != null) LogFile.WriteLine("InnerException: " + exc.InnerException);
err = true;
if (fs != null)
{
fs.Dispose();
}
System.Threading.Thread.Sleep(500);
}
}
fs.Dispose();
if (err) System.Windows.MessageBox.Show("Daten konnten nicht geschrieben werden!\nBitte erneut versuchen\n\nWenn dieser Fehler mehrmals direkt hintereinander auftritt, bitte an Marcus Bachler wenden.", "Fehler", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Warning, System.Windows.MessageBoxResult.OK);
return null;
}
}
}