0
bool a = File.Exists(archiveDetail["downloadPath"]);

The a is always set to false, even when the file specified is present in the location, when I debug and copy the file path and paste it in the explorer the file is present and its opening, but this methods returns false. Also the length file path is 309 characters, is that the cause of this?

Should the length of the file path be less than 260 characters? Also I have changed the app.confing to consider the files with long paths :

<runtime>
    <AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false;Switch.System.IO.BlockLongPaths=false" />
</runtime>

Why is File.Exists() returning false?

Tried to handle the file paths with more than 260 characters. P.S - the length of the name of file is 134 characters.

I also have tried this [link] Still not working though.

This is the path generated when debugged, when I paste this in explorer the file opens :

C:\Users\a.bhis\Documents\GitHub\Solutio-2000\Solutio\Solutio\Content\SDKs\web\Z10_\Solutions_ZC_265\Zrbeqtqdqkuqenle\SolutionExploer\ABC Privateino\Bhar\CasisInfo\ddtp___p123palsis01.extern_botgurndne_comfi_handbuerer_silly_zzp-e.nsf_5_ZZ73860ZZEZZZ8CZ6701244Z4646_$file_Lassiv+BN2+11-1999.pdf
Abhishek Pal
  • 74
  • 1
  • 9
  • 1
    What specifically is in `archiveDetail["downloadPath"]` ? the full path to the file or just the file name? – Lasse V. Karlsen Apr 03 '19 at 08:31
  • 1
    Is it in a mapped drive, or a "virtual folder" e.g. the user's Documents folder? A system account can have a different view of things than a user account. – Matthew Watson Apr 03 '19 at 08:32
  • @LasseVågsætherKarlsen the full path. P.S - the length of the name of file is 134 characters. – Abhishek Pal Apr 03 '19 at 08:33
  • 3
    Also check that the application is running with sufficent permission to access the file. If it has no access to read it, it doesn't exist. – Chrᴉz remembers Monica Apr 03 '19 at 08:34
  • @MatthewWatson no its not in a virtual folder, I am running a web application on local host and the file is in my C drive as specified by the web config. When I copy and paste the path returned by 'archiveDetail["downloadPath"]' and paste it in explorer it opens the file. P.S - the length of the name of file is 134 characters. Is it because of the length of file name? – Abhishek Pal Apr 03 '19 at 08:35
  • Also the file name has a '+' symbol in it. – Abhishek Pal Apr 03 '19 at 08:36
  • @Chrᴉz yes the applciation is running with sufficient permission. Other files are being read which have file path less than 260 characters. – Abhishek Pal Apr 03 '19 at 08:37
  • @AbhishekPal did you try to start Visual Studio as Administrator and then start the solution in Debug mode? – Dave Apr 03 '19 at 08:39
  • @JoKeRxbLaCk yes, This is the path generated when debugged, when I paste this in explorer the file opens : C:\Users\a.bhis\Documents\GitHub\Solutio-2000\Solutio\Solutio\Content\SDKs\web\Z10_\Solutions_ZC_265\Zrbeqtqdqkuqenle\SolutionExploer\ABC Privateino\Bhar\CasisInfo\ddtp___p123palsis01.extern_botgurndne_comfi_handbuerer_silly_zzp-e.nsf_5_ZZ73860ZZEZZZ8CZ6701244Z4646_$file_Lassiv+BN2+11-1999.pdf – Abhishek Pal Apr 03 '19 at 08:49
  • Could you try to rename the file with the same amount of characters but with the same character? For example 134 times the letter `a`. Maybe there is a problem with some special characters in the filename. – Dave Apr 03 '19 at 08:50
  • Did you check the value of this `archiveDetail["downloadPath"]` – Vijunav Vastivch Apr 03 '19 at 08:52
  • 1
    @VijunavVastivch the value is this - "C:\Users\a.bhis\Documents\GitHub\Solutio-2000\Solutio\Solutio\Content\SDKs\web\Z10_\Solutions_ZC_265\Zrbeqtqdqkuqenle\SolutionExploer\ABC Privateino\Bhar\CasisInfo\ddtp___p123palsis01.extern_botgurndne_comfi_handbuerer_silly_zzp-e.nsf_5_ZZ73860ZZEZZZ8CZ6701244Z4646_$file_Lassiv+BN2+11-1999.pdf" – Abhishek Pal Apr 03 '19 at 08:55
  • @JoKeRxbLaCk Thats the problem, I can not change the file name, as its a client file. Also when I try renaming the file to 134 a in windows 10, it stops after 73 characters. I also have tried this [link](https://blogs.msdn.microsoft.com/jeremykuhne/2016/07/30/net-4-6-2-and-long-paths-on-windows-10/#comment-935) Still not working though. – Abhishek Pal Apr 03 '19 at 08:57
  • Given that the file is in a Users folder, I'm guessing that this is a permissions issue. – Matthew Watson Apr 03 '19 at 09:14
  • I think so @Matthew Watson because iv'e tried same filename in my own but it returns `true` using `@`. then the string value. – Vijunav Vastivch Apr 03 '19 at 09:17
  • @MatthewWatson but the same code works for the file in same location but the name having less characters than this file. Vijunav, its not because of @. I've tried further and found that it is related to length of file name, because placing the same file on desktop(because the folder structure is less and so the file path length will be limited) and then accessing it the method returns true. – Abhishek Pal Apr 03 '19 at 09:25
  • If you do `var bytes = File.ReadAllBytes(archiveDetail["downloadPath"]);` , does it read the file? `File.Exists` requires you to have permissions to read the file, although it sounds like you're aware of that. – Sean Reid Apr 03 '19 at 09:31
  • Also - try adding the string \\?\ to the start of the file path, see https://stackoverflow.com/questions/5188527/how-to-deal-with-files-with-a-name-longer-than-259-characters . File.ReadAllBytes will throw with a long file name according to the docs, but it works if you add \\?\ to the start. – Sean Reid Apr 03 '19 at 09:40
  • did you try to run visual studio as administrator or the exe you are running from . you can also check on the permission on the folder – Mahmoud-Abdelslam Apr 03 '19 at 09:54
  • @SeanReid thanks, but \\?\ didn't work for .net 4.5.1, also the links provided strangely didn't work with all the web.config changes. – Abhishek Pal Apr 04 '19 at 06:16

1 Answers1

0

I've tried and tested this, and it works for accessing files that has long sub-directory structure thus exceeding the path length limit in windows.

For File.Exist() method, you can use this alternative method:

  if (!CheckPath(archiveDetail["downloadPath"]))
     {
      throw new Exception("The export file does not exist!");
    }

 public bool CheckPath(string path)
        {
            if (path.Length >= MAX_PATH)
            {
                return checkFile_LongPath(path);
            }
            else if (!File.Exists(path))
            {
                return false;
            }
            return false;
        }

private bool checkFile_LongPath(string path)
        {
            string[] subpaths = path.Split('\\');
            StringBuilder sbNewPath = new StringBuilder(subpaths[0]);
            // Build longest subpath that is less than MAX_PATH characters
            for (int i = 1; i < subpaths.Length; i++)
            {
                if (sbNewPath.Length + subpaths[i].Length >= MAX_PATH)
                {
                    subpaths = subpaths.Skip(i).ToArray();
                    break;
                }
                sbNewPath.Append("\\" + subpaths[i]);
            }
            DirectoryInfo dir = new DirectoryInfo(sbNewPath.ToString());
            bool foundMatch = dir.Exists;
            if (foundMatch)
            {
                // Make sure that all of the subdirectories in our path exist.
                // Skip the last entry in subpaths, since it is our filename.
                // If we try to specify the path in dir.GetDirectories(), 
                // We get a max path length error.
                int i = 0;
                while (i < subpaths.Length - 1 && foundMatch)
                {
                    foundMatch = false;
                    foreach (DirectoryInfo subDir in dir.GetDirectories())
                    {
                        if (subDir.Name == subpaths[i])
                        {
                            // Move on to the next subDirectory
                            dir = subDir;
                            foundMatch = true;
                            break;
                        }
                    }
                    i++;
                }
                if (foundMatch)
                {
                    foundMatch = false;
                    // Now that we've gone through all of the subpaths, see if our file exists.
                    // Once again, If we try to specify the path in dir.GetFiles(), 
                    // we get a max path length error.
                    foreach (FileInfo fi in dir.GetFiles())
                    {
                        if (fi.Name == subpaths[subpaths.Length - 1])
                        {
                            foundMatch = true;
                            break;
                        }
                    }
                }
            }
            // If we didn't find a match, write to the console.
            if (!foundMatch)
            {
                return false;
            }
            return foundMatch;
        }

This is as per the reference LongFileIssue

The issue of reading the file can be resolved by using the alternative class file LongPath as mentioned in this blog

While using the FileStream however you have to specify the file name, since the LongPath's FileStream name will not be as required.