0

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.

larsmoa
  • 12,604
  • 8
  • 62
  • 85
  • This sounds more like an administration question - that's how SMB works to support case -INsensitive clients like Windows. Check [this](http://superuser.com/questions/431342/linux-both-case-sensitive-and-case-insensitive-and-always-inconvenient) question in superuser. For Windows and SMB `P:\files\OUTPUT` and `p:\files\output` are the same – Panagiotis Kanavos Mar 09 '15 at 12:53

2 Answers2

1

That's the default SAMBA behaviour:

That which is lexically first will be accessible to MS Windows users; the others are invisible and unaccessible any other solution would be suicidal.

The only safe option is to use different names for the folder and file. Trying to ensure that one or the other is lexically first is (per the docs) ... suicidal.

EDIT

From the comments, it appears that Notepad can open the correct file. Despite its simplistic look, Notepad does a lot of work to handle complex cases, like detecting a file's when no BOM is available.

It may also be using long Unicode paths (eg \\?\P:\files\OUTPUT) to access files, alternate streams and shares, or it may be detecting that a network volume is in use and switch to the long path format.

System.IO doesn't support this as it's NTFS specific, but the the open-source AlphaFS provides access to this and a LOT of other useful NTFS functionality like transactions and Object IDs.

You may be able to use AlphaFS to open the file using a long path, although I haven't tried this.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • Do you have any explanation as to why e.g. Notepad is able to open the file? Pure luck? – larsmoa Mar 09 '15 at 13:16
  • 1
    Not exactly, although Notepad does a **lot** of work to handle strange eg. Unicode files without BOM. It may be using long paths for shares (starting with \\?\) although I haven't checked. What filename does your code return? `OUTPUT` or `ouput`? Also, did you check the attributes of the returned FileInfo object? Is `FileInfo.Exists` true? – Panagiotis Kanavos Mar 09 '15 at 13:31
-1
  1. Check Security access permission of file.