Skip to content

A Swiss Army knife for developers.

Fixed FileInfo detection in CLI

Etienne Baudoux91278c273402
diff --git a/src/app/dev/DevToys.Tools/Tools/EncodersDecoders/EncodersDecoders.resx b/src/app/dev/DevToys.Tools/Tools/EncodersDecoders/EncodersDecoders.resx
index 36edfb868..e57e697d9 100644
--- a/src/app/dev/DevToys.Tools/Tools/EncodersDecoders/EncodersDecoders.resx
+++ b/src/app/dev/DevToys.Tools/Tools/EncodersDecoders/EncodersDecoders.resx
@@ -118,7 +118,7 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="AccessibleName" xml:space="preserve">
- <value />
+ <value>Encoders and decoders</value>
</data>
<data name="DisplayTitle" xml:space="preserve">
<value>Encoders / Decoders</value>
diff --git a/src/app/dev/platforms/desktop/DevToys.CLI/Core/FileStorage/FileStorage.cs b/src/app/dev/platforms/desktop/DevToys.CLI/Core/FileStorage/FileStorage.cs
index 0f1bd3e31..6fb2df010 100644
--- a/src/app/dev/platforms/desktop/DevToys.CLI/Core/FileStorage/FileStorage.cs
+++ b/src/app/dev/platforms/desktop/DevToys.CLI/Core/FileStorage/FileStorage.cs
@@ -65,10 +65,12 @@ internal sealed class FileStorage : IFileStorage
{
Console.WriteLine(CliStrings.PromptSaveFile);
string? filePath = Console.ReadLine();
+
if (string.IsNullOrWhiteSpace(filePath))
{
return new ValueTask<Stream?>(Task.FromResult<Stream?>(null));
}
+
return new ValueTask<Stream?>(File.OpenWrite(filePath!));
}
diff --git a/src/app/dev/platforms/desktop/DevToys.CLI/Core/OneOfParser.cs b/src/app/dev/platforms/desktop/DevToys.CLI/Core/OneOfParser.cs
index 2776d582e..3348a57f3 100644
--- a/src/app/dev/platforms/desktop/DevToys.CLI/Core/OneOfParser.cs
+++ b/src/app/dev/platforms/desktop/DevToys.CLI/Core/OneOfParser.cs
@@ -72,13 +72,17 @@ internal static class OneOfParser
[typeof(DirectoryInfo)] = (string path, [NotNullWhen(true)] out object? value) =>
{
- if (string.IsNullOrEmpty(path))
+ value = default;
+ if (!string.IsNullOrEmpty(path))
{
- value = default;
- return false;
+ var directory = new DirectoryInfo(path);
+ if (directory.Exists)
+ {
+ value = directory;
+ return true;
+ }
}
- value = new DirectoryInfo(path);
- return true;
+ return false;
},
[typeof(double)] = (string input, [NotNullWhen(true)] out object? value) =>
@@ -95,13 +99,17 @@ internal static class OneOfParser
[typeof(FileInfo)] = (string path, [NotNullWhen(true)] out object? value) =>
{
- if (string.IsNullOrEmpty(path))
+ value = default;
+ if (!string.IsNullOrEmpty(path))
{
- value = default;
- return false;
+ var fileInfo = new FileInfo(path);
+ if (fileInfo.Exists)
+ {
+ value = fileInfo;
+ return true;
+ }
}
- value = new FileInfo(path);
- return true;
+ return false;
},
[typeof(FileSystemInfo)] = (string path, [NotNullWhen(true)] out object? value) =>
@@ -118,11 +126,23 @@ internal static class OneOfParser
else if (path.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal) ||
path.EndsWith(Path.AltDirectorySeparatorChar.ToString(), StringComparison.Ordinal))
{
- value = new DirectoryInfo(path);
+ var directory = new DirectoryInfo(path);
+ if (!Directory.Exists(path))
+ {
+ value = default;
+ return false;
+ }
+ value = directory;
}
else
{
- value = new FileInfo(path);
+ var fileInfo = new FileInfo(path);
+ if (!fileInfo.Exists)
+ {
+ value = default;
+ return false;
+ }
+ value = fileInfo;
}
return true;
@@ -188,7 +208,6 @@ internal static class OneOfParser
return false;
},
-#if NET6_0_OR_GREATER
[typeof(TimeOnly)] = (string input, [NotNullWhen(true)] out object? value) =>
{
if (TimeOnly.TryParse(input, out TimeOnly parsed))
@@ -200,7 +219,6 @@ internal static class OneOfParser
value = default;
return false;
},
-#endif
[typeof(uint)] = (string token, [NotNullWhen(true)] out object? value) =>
{