list directories from UNC drive

peterg2000

New member
Joined
Nov 18, 2021
Messages
4
Programming Experience
3-5
Hi ,

I need to list and access files on an UNC drive. (shared drive such as \\remoteserver\directoryx\)

List directories:
        public IActionResult Index()
        {
            string[] filepaths = Directory.GetDirectories(Path.GetFullPath("J:\\"));
            //Server.MapPath("~/UploadedFiles")
            List<FileViewModel> lstFiles = new List<FileViewModel>();
            foreach (var file in filepaths)
            {
                lstFiles.Add(new FileViewModel { FileName = Path.GetFileName(file) });
            }
            return View(lstFiles);
        }

This correctly lists all the directories using the drive letter but I need to use the \\remoteserver\directoryx\ format as drive letters are different on each computer.
I get drive not found error with the UNC.

Any suggestions.
This is under ASP.NET core mvc 5.0
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,029
Location
Chesapeake, VA
Programming Experience
10+
Are you correctly escaping the backslashes?

Also your code above will be running on an IIS Server. Does the app pool identity that is running your code have rights to access those network shares?
 

peterg2000

New member
Joined
Nov 18, 2021
Messages
4
Programming Experience
3-5
Are you correctly escaping the backslashes?

Also your code above will be running on an IIS Server. Does the app pool identity that is running your code have rights to access those network shares?
Are you correctly escaping the backslashes?

Also your code above will be running on an IIS Server. Does the app pool identity that is running your code have rights to access those network shares?
Yes as I've tried every combination. As for the permissions,not sure if it matters but we use windows authentication to the different drive authentications. There is no indication of authentication errors.
I can access the drives via drive letters and UNC with no problem.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,029
Location
Chesapeake, VA
Programming Experience
10+
I can access the drives via drive letters and UNC with no problem.
Yes, _you_ can, but, can the app pool identity?

 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,029
Location
Chesapeake, VA
Programming Experience
10+
Yes as I've tried every combination.
Show us your code where you have the escaped backslashes.

Also why are you calling Path.GetFullPath("J:\\") ? "J:\" is already a full path.

Or better yet, instead of having to do:
C#:
Directory.GetDirectories("\\\\server\\share")
you could do:
C#:
Directory.GetDirectories(@"\\server\share")
Personally, I find the latter improves readability.
 

peterg2000

New member
Joined
Nov 18, 2021
Messages
4
Programming Experience
3-5
Yes , I re-read your post and was looking into it , but how do I check if the app pool can access the drives?

Yes as I've tried every combination. As for the permissions,not sure if it matters but we use windows authentication to the different drive authentications. There is no indication of authentication errors.
I can access the drives via drive letters and UNC with no problem.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,029
Location
Chesapeake, VA
Programming Experience
10+
how do I check if the app pool can access the drives?
Specifically this:

On the file share, there's 2 things to check:
In the Share tab, check the permissions. Make sure it has read permissions given to domain\machine$ if you are using the IIS defaults, or the service account or user used to run the app pool if you aren't using the IIS defaults.
In the Security tab, check the permissions. Make sure it has read permissions given to domain\machine$ if you are using the IIS defaults, or the service account or user used to run the app pool if you aren't using the IIS defaults.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,029
Location
Chesapeake, VA
Programming Experience
10+
Anyway, that's the permissions side of the problem. It shouldn't matter too much while you are developing and running IISExpress on your own machine since the code will be running with your identity, and as you said, you have access.

So can you show use examples of how your were ensuring that the backslash we escaped in your string with the hard coded path? Did you look at the bottom of post #5 above?
 
Top Bottom