소스 검색

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);
         }