diff --git a/ShareX.HelpersLib/EncoderProgram.cs b/ShareX.HelpersLib/EncoderProgram.cs index df9f729e9..1441459d1 100644 --- a/ShareX.HelpersLib/EncoderProgram.cs +++ b/ShareX.HelpersLib/EncoderProgram.cs @@ -54,7 +54,7 @@ public void Encode(string sourceFilePath, string targetFilePath) targetFilePath += "." + OutputExtension.TrimStart('.'); } - Helpers.CreateDirectoryIfNotExist(targetFilePath); + Helpers.CreateDirectoryFromFilePath(targetFilePath); using (Process process = new Process()) { diff --git a/ShareX.HelpersLib/Extensions/StreamExtensions.cs b/ShareX.HelpersLib/Extensions/StreamExtensions.cs index bf562865a..83e4c5e70 100644 --- a/ShareX.HelpersLib/Extensions/StreamExtensions.cs +++ b/ShareX.HelpersLib/Extensions/StreamExtensions.cs @@ -107,7 +107,7 @@ public static bool WriteToFile(this Stream stream, string filePath) { if (stream.Length > 0 && !string.IsNullOrEmpty(filePath)) { - Helpers.CreateDirectoryIfNotExist(filePath); + Helpers.CreateDirectoryFromFilePath(filePath); using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read)) { diff --git a/ShareX.HelpersLib/FFmpegDownloader.cs b/ShareX.HelpersLib/FFmpegDownloader.cs index e3d08a22b..d7e249096 100644 --- a/ShareX.HelpersLib/FFmpegDownloader.cs +++ b/ShareX.HelpersLib/FFmpegDownloader.cs @@ -69,7 +69,7 @@ public static bool ExtractFFmpeg(string zipPath, string extractPath) SevenZipExtractor.SetLibraryPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "7z.dll")); } - Helpers.CreateDirectoryIfNotExist(extractPath); + Helpers.CreateDirectoryFromFilePath(extractPath); using (SevenZipExtractor zip = new SevenZipExtractor(zipPath)) { diff --git a/ShareX.HelpersLib/Helpers/Helpers.cs b/ShareX.HelpersLib/Helpers/Helpers.cs index edf8fcaa3..f340b4feb 100644 --- a/ShareX.HelpersLib/Helpers/Helpers.cs +++ b/ShareX.HelpersLib/Helpers/Helpers.cs @@ -732,27 +732,27 @@ public static bool IsFileLocked(string path) return false; } - public static void CreateDirectoryIfNotExist(string path, bool isFilePath = true) + public static void CreateDirectoryFromDirectoryPath(string path) + { + if (!string.IsNullOrEmpty(path) && !Directory.Exists(path)) + { + try + { + Directory.CreateDirectory(path); + } + catch (Exception e) + { + DebugHelper.WriteException(e); + MessageBox.Show(Resources.Helpers_CreateDirectoryIfNotExist_Create_failed_ + "\r\n\r\n" + e, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + + public static void CreateDirectoryFromFilePath(string path) { if (!string.IsNullOrEmpty(path)) { - if (isFilePath) - { - path = Path.GetDirectoryName(path); - } - - if (!string.IsNullOrEmpty(path) && !Directory.Exists(path)) - { - try - { - Directory.CreateDirectory(path); - } - catch (Exception e) - { - DebugHelper.WriteException(e); - MessageBox.Show(Resources.Helpers_CreateDirectoryIfNotExist_Create_failed_ + "\r\n\r\n" + e, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } + CreateDirectoryFromDirectoryPath(Path.GetDirectoryName(path)); } } @@ -767,7 +767,7 @@ public static void BackupFileMonthly(string filepath, string destinationFolder) if (!File.Exists(newFilepath)) { - CreateDirectoryIfNotExist(newFilepath); + CreateDirectoryFromFilePath(newFilepath); File.Copy(filepath, newFilepath, false); } } @@ -785,7 +785,7 @@ public static void BackupFileWeekly(string filepath, string destinationFolder) if (!File.Exists(newFilepath)) { - CreateDirectoryIfNotExist(newFilepath); + CreateDirectoryFromFilePath(newFilepath); File.Copy(filepath, newFilepath, false); } } diff --git a/ShareX.HelpersLib/Logger.cs b/ShareX.HelpersLib/Logger.cs index 478d39262..a99ffafc7 100644 --- a/ShareX.HelpersLib/Logger.cs +++ b/ShareX.HelpersLib/Logger.cs @@ -53,7 +53,7 @@ public Logger(string logFilePath) { FileWrite = true; LogFilePath = logFilePath; - Helpers.CreateDirectoryIfNotExist(LogFilePath); + Helpers.CreateDirectoryFromFilePath(LogFilePath); } protected void OnMessageAdded(string message) diff --git a/ShareX.HelpersLib/SettingsBase.cs b/ShareX.HelpersLib/SettingsBase.cs index 9ca8574dd..8a1a6f98f 100644 --- a/ShareX.HelpersLib/SettingsBase.cs +++ b/ShareX.HelpersLib/SettingsBase.cs @@ -121,7 +121,7 @@ private static bool SaveInternal(object obj, string filePath, bool createBackup) { lock (obj) { - Helpers.CreateDirectoryIfNotExist(filePath); + Helpers.CreateDirectoryFromFilePath(filePath); string tempFilePath = filePath + ".temp"; diff --git a/ShareX.HistoryLib/XMLManager.cs b/ShareX.HistoryLib/XMLManager.cs index cb1ebddc8..28d966284 100644 --- a/ShareX.HistoryLib/XMLManager.cs +++ b/ShareX.HistoryLib/XMLManager.cs @@ -93,7 +93,7 @@ public bool Append(params HistoryItem[] historyItems) { lock (thisLock) { - Helpers.CreateDirectoryIfNotExist(FilePath); + Helpers.CreateDirectoryFromFilePath(FilePath); using (FileStream fs = File.Open(FilePath, FileMode.Append, FileAccess.Write, FileShare.Read)) using (XmlTextWriter writer = new XmlTextWriter(fs, Encoding.UTF8)) diff --git a/ShareX.MediaLib/FFmpegCLIManager.cs b/ShareX.MediaLib/FFmpegCLIManager.cs index 7f5d4c004..29470e6d1 100644 --- a/ShareX.MediaLib/FFmpegCLIManager.cs +++ b/ShareX.MediaLib/FFmpegCLIManager.cs @@ -44,7 +44,7 @@ public FFmpegCLIManager(string ffmpegPath) Output = new StringBuilder(); OutputDataReceived += FFmpeg_DataReceived; ErrorDataReceived += FFmpeg_DataReceived; - Helpers.CreateDirectoryIfNotExist(FFmpegPath); + Helpers.CreateDirectoryFromFilePath(FFmpegPath); } private void FFmpeg_DataReceived(object sender, DataReceivedEventArgs e) diff --git a/ShareX.MediaLib/VideoThumbnailer.cs b/ShareX.MediaLib/VideoThumbnailer.cs index b92bf1660..d2a3cad62 100644 --- a/ShareX.MediaLib/VideoThumbnailer.cs +++ b/ShareX.MediaLib/VideoThumbnailer.cs @@ -162,7 +162,7 @@ private string GetOutputDirectory() break; } - Helpers.CreateDirectoryIfNotExist(directory, false); + Helpers.CreateDirectoryFromDirectoryPath(directory); return directory; } diff --git a/ShareX.ScreenCaptureLib/Screencast/FFmpegHelper.cs b/ShareX.ScreenCaptureLib/Screencast/FFmpegHelper.cs index a34b77ef3..b2858b220 100644 --- a/ShareX.ScreenCaptureLib/Screencast/FFmpegHelper.cs +++ b/ShareX.ScreenCaptureLib/Screencast/FFmpegHelper.cs @@ -58,7 +58,7 @@ public FFmpegHelper(ScreencastOptions options) OutputDataReceived += FFmpegHelper_DataReceived; ErrorDataReceived += FFmpegHelper_DataReceived; Options = options; - Helpers.CreateDirectoryIfNotExist(Options.OutputPath); + Helpers.CreateDirectoryFromFilePath(Options.OutputPath); } private void FFmpegHelper_DataReceived(object sender, DataReceivedEventArgs e) diff --git a/ShareX.ScreenCaptureLib/Screencast/HardDiskCache.cs b/ShareX.ScreenCaptureLib/Screencast/HardDiskCache.cs index 4171afd14..715f1cdfe 100644 --- a/ShareX.ScreenCaptureLib/Screencast/HardDiskCache.cs +++ b/ShareX.ScreenCaptureLib/Screencast/HardDiskCache.cs @@ -52,7 +52,7 @@ public int Count public HardDiskCache(ScreencastOptions options) { Options = options; - Helpers.CreateDirectoryIfNotExist(Options.OutputPath); + Helpers.CreateDirectoryFromFilePath(Options.OutputPath); fsCache = new FileStream(Options.OutputPath, FileMode.Create, FileAccess.Write, FileShare.Read); indexList = new List(); } diff --git a/ShareX.ScreenCaptureLib/Screencast/ScreenRecorder.cs b/ShareX.ScreenCaptureLib/Screencast/ScreenRecorder.cs index 29b2e9e75..18c7ded2d 100644 --- a/ShareX.ScreenCaptureLib/Screencast/ScreenRecorder.cs +++ b/ShareX.ScreenCaptureLib/Screencast/ScreenRecorder.cs @@ -204,7 +204,7 @@ public void SaveAsGIF(string path, GIFQuality quality) { if (imgCache != null && imgCache is HardDiskCache && !IsRecording) { - Helpers.CreateDirectoryIfNotExist(path); + Helpers.CreateDirectoryFromFilePath(path); HardDiskCache hdCache = imgCache as HardDiskCache; @@ -229,7 +229,7 @@ public void SaveAsGIF(string path, GIFQuality quality) public bool FFmpegEncodeAsGIF(string path) { - Helpers.CreateDirectoryIfNotExist(path); + Helpers.CreateDirectoryFromFilePath(path); return ffmpegCli.EncodeGIF(Options.OutputPath, path); } diff --git a/ShareX.UploadersLib/FileUploaders/SharedFolderUploader.cs b/ShareX.UploadersLib/FileUploaders/SharedFolderUploader.cs index 57dde3716..99939700f 100644 --- a/ShareX.UploadersLib/FileUploaders/SharedFolderUploader.cs +++ b/ShareX.UploadersLib/FileUploaders/SharedFolderUploader.cs @@ -43,7 +43,7 @@ public override UploadResult Upload(Stream stream, string fileName) string filePath = account.GetLocalhostPath(fileName); - Helpers.CreateDirectoryIfNotExist(filePath); + Helpers.CreateDirectoryFromFilePath(filePath); using (FileStream fs = new FileStream(filePath, FileMode.Create)) { diff --git a/ShareX/Forms/ChromeForm.cs b/ShareX/Forms/ChromeForm.cs index cd41b7a03..4989edc10 100644 --- a/ShareX/Forms/ChromeForm.cs +++ b/ShareX/Forms/ChromeForm.cs @@ -42,7 +42,7 @@ public ChromeForm() private void CreateChromeHostManifest(string filepath) { - Helpers.CreateDirectoryIfNotExist(filepath); + Helpers.CreateDirectoryFromFilePath(filepath); var manifest = new { diff --git a/ShareX/WorkerTask.cs b/ShareX/WorkerTask.cs index a4d711439..51cca565a 100644 --- a/ShareX/WorkerTask.cs +++ b/ShareX/WorkerTask.cs @@ -672,7 +672,7 @@ private void DoTextJobs() if (!string.IsNullOrEmpty(filePath)) { Info.FilePath = filePath; - Helpers.CreateDirectoryIfNotExist(Info.FilePath); + Helpers.CreateDirectoryFromFilePath(Info.FilePath); File.WriteAllText(Info.FilePath, tempText, Encoding.UTF8); DebugHelper.WriteLine("Text saved to file: " + Info.FilePath); } @@ -1361,7 +1361,7 @@ private bool DownloadAndUpload() try { - Helpers.CreateDirectoryIfNotExist(Info.FilePath); + Helpers.CreateDirectoryFromFilePath(Info.FilePath); using (WebClient wc = new WebClient()) {