Reputation: 24789
With this code:
DirectoryInfo info = new DirectoryInfo("\\s01\sharedfolder\folder");
FileInfo[] files = info.GetFiles().OrderBy(p => p.CreationTime).ToArray();
I get a System.UnauthorizedAccessException
:
Access to the path '\s01sharedfolder\folder' is denied.
System.UnauthorizedAccessException: Access to the path '\s01\sharedfolder\folder' is denied.
On 's01' I have given Everyone
full control to the sharedfolder
. I also have set the owner of that shared folder to everyone
.
The identity of the application pool of the .net app is set to LocalSystem
and I have also tried to use the local administrator as the identity.
No matter what I try, I keep getting this exception. What do I need to do to fix this?
EDIT: I have also tried to create a symbolic link, but this gave the same exception
Upvotes: 3
Views: 2584
Reputation: 1207
It might be the expected behavior for using File.OpenRead rather than a permission issue. File.OpenRead tries to open a file with that name in the local machine. Most of the time, the files are in the server, not in the local (client) machines. Therefore, it fails no matter what permissions are assigned to the folder. Try InputStream instead. Example:
String fileToUpload = FileUpload2.PostedFile.FileName;
long contentLength = FileUpload2.PostedFile.InputStream.Length;
byte[] buffer = new byte[contentLength];
FileUpload2.PostedFile.InputStream.Seek(0, SeekOrigin.Begin);
FileUpload2.PostedFile.InputStream.Read(buffer, 0, Convert.ToInt32(contentLength));
Stream stream = new MemoryStream(buffer);
Source: System.UnauthorizedAccessException occurred in mscorlib.dll
Upvotes: 0
Reputation: 5838
LocalSystem will only be valid on your local machine. If the share is on another computer then you will get this exception.
It might be worth setting up a dedicated account for your site to run under (IIS Pool) and granting the required permissions to resources on the network (ala your file share and database if using Windows Authentication) and local to your site (ala local file system).
Granted when running as System you get admin rights to that local machine only (I think).
Upvotes: 2