Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directory exists is producing inconsistant results on desktop vs server

Tags:

c#

windows

I have a C# program which checks if a specific directory exists.

It is simply doing:

Directory.Exists(path).

I tried other ways as well. Using DirectoryInfo and using AlphaFS

On my local machine, the path exists. When I run the same program on a server with the same credentials it doesn't exist.

I wonder if it is a group policy issue. But I am able to go up one level and see it.

\server\volume\share\sub directory - Doesn't exist remotely but on my desktop it does \server\volume\share - Does exist both on my desktop and remote server

Update I forgot to mention, that since I had access to my desktop, I got the ACL information. None of the groups were able to translate.

I really just want to get this application to behave the same way is on the server and find out why it is behaving differently.

Update 2 These are physical servers. My desktop is Liquid VDI

Below is the code:

 var path = txtPath.Text;
        using (var user = new Impersonation(fuserdomain, fc_user, fc_pass))
        {
            var alphaExists = Alphaleonis.Win32.Filesystem.Directory.Exists(path);
            var alphaDIExists = new Alphaleonis.Win32.Filesystem.DirectoryInfo(path).Exists;
            var SystemExists = System.IO.Directory.Exists(path);
            var SystemDIExists = new System.IO.DirectoryInfo(path).Exists;
            var AlphaHasFiles = false;
            var AlphaDIHasFiles = false;
            var SystemHasFiles = false;
            var SystemDIHasFiles = false;
            try
            {
                Directory.GetFiles(path);
                AlphaHasFiles = true;
            }
            catch {  }
            try
            {
                new DirectoryInfo(path).GetFiles();
                AlphaDIHasFiles = true;
            }
            catch {  }
            try
            {
                System.IO.Directory.GetFiles(path);
                SystemHasFiles = true;
            }
            catch { }
            try
            {
                new System.IO.DirectoryInfo(path).GetFiles();
                SystemDIHasFiles = true;
            }
            catch { }

            MessageBox.Show(string.Format("alphaExists: {0}\nalphaDIExists: {1}\nSystemExists: {2}\nSystemDIExists: {3}\nAlphaGetFiles: {4}\nAlphaDIGetFiles: {5}\nSystemGetFiles: {6}\nSystemDIGetFiles: {7}\n", alphaExists, alphaDIExists, SystemExists, SystemDIExists, AlphaHasFiles, AlphaDIHasFiles, SystemHasFiles, SystemDIHasFiles));
        }

Update 3 Although I have workaround this issue; I am still not sure why I would have a difference between my desktop and server. Is there any tool that can help me see where the issue may be?

like image 520
H20rider Avatar asked Feb 23 '18 17:02

H20rider


2 Answers

I've seen the same thing with File.Exists. I never found an answer and finally threw in the towel, I simply try to use it and catch the exception.

Robust code has to catch it anyway, all the test does is avoid trying if the file or directory is not there. (And the PITA that Visual Studio no longer as any way to ignore an exception on a certain line. No problem runtime, annoying in development.)

like image 124
Loren Pechtel Avatar answered Oct 21 '22 05:10

Loren Pechtel


This is a complete shot in the dark, since we don't have any specific details to go on. e.g. Is the server you're talking about physically yours, or is it a cloud-based server service?

I'd guess that your machine is an older operating system than the server, and the folder that you're trying to access is one of those special folders that has become more locked down with more recent operating systems (particularly on server operating systems) like the "Program Files" folder. So even though the folder exists on both, the method works on your machine but not on the server, due to permissions.

Hope this helps.

like image 36
Richardissimo Avatar answered Oct 21 '22 05:10

Richardissimo