Browse Source

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 years ago
parent
commit
d00deb239b
1 changed files with 19 additions and 1 deletions
  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
             // 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
             // Note that we're using Pri.LongPath to get a Path.GetDirectoryName implementation that can handle
             // long paths
             // 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);
             this.PathChanged(oldFullPath, fileExists: false);
         }
         }