Skip to content

Commit 90f7627

Browse files
committed
🐞 FileHelper: skip replacing when the destination file doesn't exist
File.Replace throws an exception when the destination file does not exist.
1 parent 6a46513 commit 90f7627

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

YoutubeDl.Wpf/Utils/FileHelper.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,17 @@ public static async Task SaveToJsonFileAsync<TValue>(
7676

7777
_ = Directory.CreateDirectory(directoryPath);
7878

79-
var newPath = $"{path}.new";
79+
// File.Replace throws an exception when the destination file does not exist.
80+
var canReplace = File.Exists(path);
81+
var newPath = canReplace ? $"{path}.new" : path;
8082
var fileStream = new FileStream(newPath, FileMode.Create);
83+
8184
await using (fileStream.ConfigureAwait(false))
8285
{
8386
await JsonSerializer.SerializeAsync(fileStream, value, jsonTypeInfo, cancellationToken);
8487
}
85-
File.Replace(newPath, path, $"{path}.old");
88+
89+
if (canReplace)
90+
File.Replace(newPath, path, $"{path}.old");
8691
}
8792
}

0 commit comments

Comments
 (0)