瀏覽代碼

Check intermediate paths in OnRenamed for null

It appears that Pri.LongPath.GetDirectoryName or Pri.LongPath.GetFileName
can sometimes return null. I'm not sure why... But let's not crash
in this case

Relates to #112
Antony Male 10 年之前
父節點
當前提交
d00deb239b
共有 1 個文件被更改,包括 19 次插入1 次删除
  1. 19 1
      src/SyncTrayzor/Services/DirectoryWatcher.cs

+ 19 - 1
src/SyncTrayzor/Services/DirectoryWatcher.cs

@@ -138,7 +138,25 @@ namespace SyncTrayzor.Services
             // So, construct it from e.FullPath and e.OldName
             // Note that we're using Pri.LongPath to get a Path.GetDirectoryName implementation that can handle
             // long paths
-            var oldFullPath = Path.Combine(Path.GetDirectoryName(e.FullPath), Path.GetFileName(e.OldName));
+
+            // Apparently Path.GetDirectoryName(e.FullPath) or Path.GetFileName(e.OldName) can return null, see #112
+            // Not sure why this might be, but let's work around it...
+            var oldFullPathDirectory = Path.GetDirectoryName(e.FullPath);
+            var oldFileName = Path.GetFileName(e.OldName);
+
+            if (oldFullPathDirectory == null)
+            {
+                logger.Warn("OldFullPathDirectory is null. Not sure why... e.FullPath: {0}", e.FullPath);
+                return;
+            }
+            
+            if (oldFileName == null)
+            {
+                logger.Warn("OldFileName is null. Not sure why... e.OldName: {0}", e.OldName);
+                return;
+            }
+
+            var oldFullPath = Path.Combine(oldFullPathDirectory, oldFileName);
 
             this.PathChanged(oldFullPath, fileExists: false);
         }