Comments (15)
I can't reproduce 1 and 2, these cases work fine for me.
As for 3, this is probably because you have something open in the source directory. You would get the same error with System.IO.Directory.Move.
from longpath.
Thanks for the super fast reply.
Please can you try out these examples to reproduce the first 2 errors:
Create : C:\Test\a folder 1\somefile.txt
Try: Directory.GetFiles("C:\Test","some_.txt",System.IO.SearchOption.AllDirectories)
Try: Directory.GetDirectories("C:\Test","_folder*",System.IO.SearchOption.AllDirectories)
The System.IO.Directory.Move works fine for me i.e. never encounters this exception. Your implementation however, fails often (~40%). There is definitely a bug here.
from longpath.
These 2 examples fail (i.e. no files or directories found) for me...
Create: C:\Test\a folder 1\b folder 2\somefile.txt
var files = Directory.GetFiles("C:\Test","some*.txt",System.IO.SearchOption.AllDirectories);
var dirs = Directory.GetDirectories("C:\Test","b folder*",System.IO.SearchOption.AllDirectories);
from longpath.
OK, I was just testing with "." as the filter... now I see the problem.
(I'm not the maintainer of this project, btw)
from longpath.
Yes, thanks for the follow up @thomaslevesque. Your day must start earlier than mine :) I gather you've reproduced it? Thanks for the report @YetAnotherPhil, I'll look into it.
from longpath.
Thanks guys, much appreciated
from longpath.
Here's a modified version of your EnumerateFileSystemIteratorRecursive (not thoroughly tested by any means!)...
private static IEnumerable<string> EnumerateFileSystemIteratorRecursive(string normalizedPath, string normalizedSearchPattern, bool includeDirectories, bool includeFiles)
{
// NOTE: Any exceptions thrown from this method are thrown on a call to IEnumerator<string>.MoveNext()
var pending = new Queue<string>();
pending.Enqueue(normalizedPath);
while (pending.Count > 0)
{
normalizedPath = pending.Dequeue();
string path = Path.RemoveLongPathPrefix(normalizedPath);
foreach(var dir in Directory.GetDirectories(path))
{
pending.Enqueue(Path.NormalizeLongPath(dir));
}
NativeMethods.WIN32_FIND_DATA findData;
using (SafeFindHandle handle = BeginFind(Path.Combine(normalizedPath, normalizedSearchPattern), out findData))
{
if (handle == null)
continue;
do
{
string currentFileName = findData.cFileName;
var fullPath = Path.Combine(path, currentFileName);
if (IsDirectory(findData.dwFileAttributes))
{
var fullNormalizedPath = Path.Combine(normalizedPath, currentFileName);
System.Diagnostics.Debug.Assert(Directory.Exists(fullPath));
System.Diagnostics.Debug.Assert(Directory.Exists(Path.RemoveLongPathPrefix(fullNormalizedPath)));
if (!IsCurrentOrParentDirectory(currentFileName))
{
//pending.Enqueue(fullNormalizedPath);
if (includeDirectories)
{
yield return fullPath;
}
}
}
else
{
if (includeFiles)
{
yield return fullPath;
}
}
} while (NativeMethods.FindNextFile(handle, out findData));
int errorCode = Marshal.GetLastWin32Error();
if (errorCode != NativeMethods.ERROR_NO_MORE_FILES)
throw Common.GetExceptionFromWin32Error(errorCode);
}
}
}
from longpath.
Thanks. I need to work through some Visual Studio issues that I've encountered with this project; but I hope to thow this on my backlog soon.
from longpath.
The Directory.Move bug... The wrong (different) exception was thrown when a file\folder is locked. Below is a modified version of your GetExceptionFromWin32Error to correct the issue:
internal static Exception GetExceptionFromWin32Error(int errorCode, string parameterName)
{
string message = GetMessageFromErrorCode(errorCode);
switch (errorCode)
{
case NativeMethods.ERROR_FILE_NOT_FOUND:
return new System.IO.FileNotFoundException(message);
case NativeMethods.ERROR_PATH_NOT_FOUND:
return new System.IO.DirectoryNotFoundException(message);
case NativeMethods.ERROR_ACCESS_DENIED:
//return new UnauthorizedAccessException(message);
return new System.IO.IOException(message);
case NativeMethods.ERROR_FILENAME_EXCED_RANGE:
return new System.IO.PathTooLongException(message);
case NativeMethods.ERROR_INVALID_DRIVE:
return new System.IO.DriveNotFoundException(message);
case NativeMethods.ERROR_OPERATION_ABORTED:
return new OperationCanceledException(message);
case NativeMethods.ERROR_INVALID_NAME:
return new ArgumentException(message, parameterName);
default:
return new System.IO.IOException(message, NativeMethods.MakeHRFromErrorCode(errorCode));
}
}
from longpath.
This change doesn't make sense, you're just hiding some information. If the system returned "access denied", it's better to throw an UnauthorizedException
; that's what the BCL is doing as well. If you just throw an IOException
, there is no way to detect that it was actually an access denied error.
from longpath.
Try this...
Create c:\test\somefile.txt
Open Explorer.exe in c:\test to lock the folder
Now run
try
{
Pri.LongPath.Directory.Move("c:\test", "c:\test2");
}
catch(Exception e)
{
var err = e.ToString();
}
try
{
System.IO.Directory.Move("c:\test", "c:\test2");
}
catch(Exception e)
{
var err = e.ToString();
}
You should see System.IO.Directory.Move raises a System.IO.IOException.
The Access Denied message isn't lost, it's just the type of exception you're throwing differs from System.IO.Directory.Move.
from longpath.
This is some hours code to expand the unit tests. I'll verify the results and evaluate how to change the code to fix this and not break something else. Thanks
from longpath.
I've committed a change that should deal with the first issue. Similar to what you posted @YetAnotherPhil, but a little more efficient.
from longpath.
I just added some unit tests to verify case two is also fixed.
from longpath.
Commit 8b6011e fixes the UnauthorizedAccessException
and includes tests to verify.
from longpath.
Related Issues (20)
- .NET Standard 2.0 support HOT 2
- DirectoryInfo.Refresh fails with UnauthorizedAccessException if executing principal has no access to parent folder HOT 1
- Path.GetPathRoot() throws ArgumentOutOfRangeException HOT 1
- The filename or extension is too long when creating a Directory on UNC
- Infinite sequence of elements when traversing the directory name consisting of single space HOT 3
- StreamWriter HOT 1
- Regression on 2.0.47.0 for DirectoryInfo HOT 2
- Can you document public methods HOT 1
- Mono support incomplete HOT 5
- File handles not being disposed HOT 2
- Path.GetPathRoot() throws IndexOutOfRangeException
- File.WriteAllText throws System.EntryPointNotFoundException on Mono HOT 1
- Endless loop
- File.Copy does two operations, for not win and then for win
- DirectoryInfo Delete mothod is throw exception HOT 1
- Have you considered merging with System.IO.Abstractions? HOT 1
- Error: Trying to create a parent directory, which exists but the current user has no permissions. HOT 1
- Consider checking for error upon subdirectory creation in Directory.CreateDirectory
- Long UNC pathname containing forward-slash (/) path separators is not normalised correctly
- Error with a really long and strange filename. HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from longpath.