Merge pull request #8899 from ivandrofly/feature/file-util

Refactor directory writable check to FileUtil
This commit is contained in:
Nikolaj Olsson 2024-10-12 18:49:09 +02:00 committed by GitHub
commit 8ff7b7abf5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 16 deletions

View File

@ -935,5 +935,23 @@ namespace Nikse.SubtitleEdit.Core.Common
return string.Empty; return string.Empty;
} }
/// <summary>
/// Checks if a directory is writable by attempting to create and delete a temporary file within it.
/// </summary>
/// <param name="dirPath">The directory path to check for write access.</param>
/// <returns>True if the directory is writable, false otherwise.</returns>
public static bool IsDirectoryWritable(string dirPath)
{
try
{
using (File.Create(Path.Combine(dirPath, Path.GetRandomFileName()), 1, FileOptions.DeleteOnClose)) { }
return true;
}
catch
{
return false;
}
}
} }
} }

View File

@ -267,7 +267,7 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText
catch catch
{ {
var dir = Path.GetDirectoryName(fileName); var dir = Path.GetDirectoryName(fileName);
if (!WhisperAudioToText.IsDirectoryWritable(dir)) if (!FileUtil.IsDirectoryWritable(dir))
{ {
MessageBox.Show($"SE does not have write access to the folder '{dir}'", MessageBoxIcon.Error); MessageBox.Show($"SE does not have write access to the folder '{dir}'", MessageBoxIcon.Error);
} }

View File

@ -843,7 +843,7 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText
catch catch
{ {
var dir = Path.GetDirectoryName(fileName); var dir = Path.GetDirectoryName(fileName);
if (!IsDirectoryWritable(dir)) if (!FileUtil.IsDirectoryWritable(dir))
{ {
MessageBox.Show($"SE does not have write access to the folder '{dir}'", MessageBoxIcon.Error); MessageBox.Show($"SE does not have write access to the folder '{dir}'", MessageBoxIcon.Error);
} }
@ -852,20 +852,7 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText
} }
} }
public static bool IsDirectoryWritable(string dirPath)
{
try
{
using (FileStream fs = File.Create(Path.Combine(dirPath, Path.GetRandomFileName()), 1, FileOptions.DeleteOnClose))
{
}
return true;
}
catch
{
return false;
}
}
internal static string GetLanguage(string name) internal static string GetLanguage(string name)
{ {