浏览代码

Create new clocks with cleaner file names

No more long GUIDs!
Daniel Chalmers 2 年之前
父节点
当前提交
314bf4f728
共有 2 个文件被更改,包括 28 次插入3 次删除
  1. 23 0
      DesktopClock/FileUtil.cs
  2. 5 3
      DesktopClock/MainWindow.xaml.cs

+ 23 - 0
DesktopClock/FileUtil.cs

@@ -0,0 +1,23 @@
+using System.IO;
+
+namespace DesktopClock;
+
+public static class FileUtil
+{
+    /// <summary>
+    /// Returns an indexed version of the filename that doesn't exist yet.
+    /// </summary>
+    public static FileInfo GetFileAtNextIndex(this FileInfo fileInfo)
+    {
+        var i = 1;
+        FileInfo file;
+        do
+        {
+            i++;
+            var baseName = Path.GetFileNameWithoutExtension(fileInfo.FullName);
+            file = new FileInfo($"{baseName}-{i}{fileInfo.Extension}");
+        } while (file.Exists);
+
+        return file;
+    }
+}

+ 5 - 3
DesktopClock/MainWindow.xaml.cs

@@ -118,9 +118,11 @@ public partial class MainWindow : Window
         if (result != MessageBoxResult.OK)
             return;
 
-        var exeInfo = new FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location);
-        var newExePath = Path.Combine(exeInfo.DirectoryName, Guid.NewGuid().ToString() + exeInfo.Name);
-        File.Copy(exeInfo.FullName, newExePath);
+        var currentExe = new FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location);
+        var newExePath = Path.Combine(currentExe.DirectoryName, currentExe.GetFileAtNextIndex().Name);
+
+        // Copy and start the new clock.
+        File.Copy(currentExe.FullName, newExePath);
         Process.Start(newExePath);
     }