I am creating a file explorer using UWP technologies.
I originally thought that I was unable to see files on a specific removable drive, but as it turns out, anything in the root folder of a drive will throw a System.IOException
I can see the other drives and access their properties, but when I read the Length
of one, it throws the following exception:
System.IO.IOException: The parameter is incorrect at System.IO.WinRTIOExtensions.d__2`1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.IO.WinRTFileSystem.WinRTFileSystemObject.d__25.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.IO.WinRTFileSystem.WinRTFileSystemObject.EnsureItemInitialized() at System.IO.WinRTFileSystem.WinRTFileSystemObject.get_Attributes() at System.IO.FileInfo.get_Length() at Traverse.MainPage.<>c__DisplayClass9_0.b__0()
Here's the code that breaks:
Task.Run(() => {
Debug.WriteLine(path);
FileInfo fileInfo = new FileInfo(path);
Debug.WriteLine(fileInfo.FullName);
Debug.WriteLine(fileInfo.Length);
});
Output:
D:\bitmap-machine.html
D:\bitmap-machine.html
(The long error message from above)
Some more code that will trigger the issue:
FileInfo f = new FileInfo(@"C:\ReadMe.txt");
Debug.WriteLine(f.Length);
Some code which will NOT trigger the issue:
FileInfo f = new FileInfo(@"C:\PHP\php.gif");
Debug.WriteLine(f.Length);
I have the removable devices capability enabled, and I can confirm it is working because I can see the devices.
I would expect an error about not having permission to the drive, but a "The parameter is incorrect" when trying to get file length is certainly odd!