more io exception fixes

This commit is contained in:
pointfeev 2023-01-29 20:30:05 -05:00
parent 3d63457a45
commit 36570420a4
3 changed files with 31 additions and 28 deletions

View file

@ -8,7 +8,6 @@ using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using CreamInstaller.Forms;
using CreamInstaller.Utility;
namespace CreamInstaller.Resources;
@ -441,29 +440,33 @@ internal static class Resources
internal static void WriteManifestResource(this string resourceIdentifier, string filePath)
{
using Stream resource = Assembly.GetExecutingAssembly().GetManifestResourceStream("CreamInstaller.Resources." + resourceIdentifier);
while (!Program.Canceled)
try
{
using FileStream file = new(filePath, FileMode.Create, FileAccess.Write);
resource?.CopyTo(file);
}
#if DEBUG
catch (Exception e)
{
DebugForm.Current.Log("resource write exception for '" + resourceIdentifier + "' to '" + filePath + "': " + e);
}
#else
catch
{
//ignored
if (filePath.IOWarn("Failed to write a crucial manifest resource") is not DialogResult.OK)
break;
}
#endif
}
internal static void WriteResource(this byte[] resource, string filePath)
{
while (!Program.Canceled)
try
{
using FileStream fileStream = new(filePath, FileMode.Create, FileAccess.Write);
fileStream.Write(resource);
}
catch
{
if (filePath.IOWarn("Failed to write a crucial resource") is not DialogResult.OK)
break;
}
}
internal static bool IsFilePathLocked(this string filePath)
{

View file

@ -45,7 +45,7 @@ internal static class ExceptionHandler
if (string.IsNullOrWhiteSpace(outputString))
outputString = e?.ToString() ?? "Unknown exception";
using DialogForm dialogForm = new(form ?? Form.ActiveForm);
return dialogForm.Show(SystemIcons.Error, outputString, acceptButtonText, cancelButtonText, caption) == DialogResult.OK;
return dialogForm.Show(SystemIcons.Error, outputString, acceptButtonText, cancelButtonText, caption) is DialogResult.OK;
}
internal static void HandleFatalException(this Exception e)

View file

@ -13,7 +13,7 @@ internal static class SafeIO
while (!Program.Canceled)
{
bool exists = File.Exists(filePath);
if (exists || !crucial || filePath.Warn("Failed to find a crucial file", form) is not DialogResult.OK)
if (exists || !crucial || filePath.IOWarn("Failed to find a crucial file", form) is not DialogResult.OK)
return exists;
}
return false;
@ -29,7 +29,7 @@ internal static class SafeIO
}
catch
{
if (!crucial || filePath.Warn("Failed to create a crucial file", form) is not DialogResult.OK)
if (!crucial || filePath.IOWarn("Failed to create a crucial file", form) is not DialogResult.OK)
break;
}
}
@ -44,7 +44,7 @@ internal static class SafeIO
}
catch
{
if (!crucial || !filePath.Exists(true) || filePath.Warn("Failed to move a crucial file", form) is not DialogResult.OK)
if (!crucial || !filePath.Exists(true) || filePath.IOWarn("Failed to move a crucial file", form) is not DialogResult.OK)
break;
}
}
@ -61,7 +61,7 @@ internal static class SafeIO
}
catch
{
if (!crucial || filePath.Warn("Failed to delete a crucial file", form) is not DialogResult.OK)
if (!crucial || filePath.IOWarn("Failed to delete a crucial file", form) is not DialogResult.OK)
break;
}
}
@ -75,7 +75,7 @@ internal static class SafeIO
}
catch
{
if (!crucial || !filePath.Exists(true) || filePath.Warn("Failed to read a crucial file", form) is not DialogResult.OK)
if (!crucial || !filePath.Exists(true) || filePath.IOWarn("Failed to read a crucial file", form) is not DialogResult.OK)
break;
}
return null;
@ -90,7 +90,7 @@ internal static class SafeIO
}
catch
{
if (!crucial || !filePath.Exists(true) || filePath.Warn("Failed to read a crucial file", form) is not DialogResult.OK)
if (!crucial || !filePath.Exists(true) || filePath.IOWarn("Failed to read a crucial file", form) is not DialogResult.OK)
break;
}
return null;
@ -106,12 +106,12 @@ internal static class SafeIO
}
catch
{
if (!crucial || filePath.Warn("Failed to write a crucial file", form) is not DialogResult.OK)
if (!crucial || filePath.IOWarn("Failed to write a crucial file", form) is not DialogResult.OK)
break;
}
}
private static DialogResult Warn(this string filePath, string message, Form form = null)
internal static DialogResult IOWarn(this string filePath, string message, Form form = null)
{
using DialogForm dialogForm = new(form ?? Form.ActiveForm);
return dialogForm.Show(SystemIcons.Warning, message + ": " + filePath.BeautifyPath(), "Retry", "OK");