When invoking File.OpenRead()
on a file on a network share where a folder with the same name (but different casing) exists, UnauthorizedAccessException
. This can happen on Linux shares where casing matters.
Example: * P: is mapped to \somemachine\someshare * P:\files\ is a folder * P:\files\OUTPUT is a file * P:\files\output is a folder
The following code will throw:
const string path = @"P:\files\OUTPUT";
DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(path));
FileInfo file = dir.EnumerateFiles().FirstOrDefault(x => string.Equals(Path.GetFileName(path), x.Name));
// All of the below throws UnauthorizedAccessException
file.OpenRead();
FileStream stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
File.OpenRead(path);
Is there any way I can open the file case sensitively? It's not an option to rename the file or move the folder out of the way as this is a read only share.