2

My project is not finding files that definitely exist. I am using the Stanford NLP library and was getting file not found exceptions which I began debugging.

Here is my test code:

String jarRoot = @"stanford-corenlp-full-2016-10-31\stanford-corenlp-full-2016-10-31\stanford-corenlp-3.7.0-models\edu\stanford\nlp\models\pos-tagger\english-left3words\";
foreach (String fName in Directory.GetFiles(jarRoot))
{
    Console.WriteLine("File in jarRoot: " + fName);
    Console.WriteLine("File exists? " + File.Exists(fName));
}

The output:

File in jarRoot: stanford-corenlp-full-2016-10-31\stanford-corenlp-full-2016-10-31\stanford-corenlp-3.7.0-models\edu\stanford\nlp\models\pos-tagger\english-left3words\english-left3words-distsim.tagger
File exists? False
File in jarRoot: stanford-corenlp-full-2016-10-31\stanford-corenlp-full-2016-10-31\stanford-corenlp-3.7.0-models\edu\stanford\nlp\models\pos-tagger\english-left3words\english-left3words-distsim.tagger.props
File exists? False

How could File.Exists() possibly be returning false?

Screenshots of directory: enter image description here

blagh
  • 71
  • 1
  • 1
  • 10
  • jar root is not a correct physical path please make sure the path is correct. –  Jun 16 '17 at 07:54
  • @TAHASULTANTEMURI It's a perfectly valid path, not sure what you mean. – Rob Jun 16 '17 at 07:54
  • First try open run command and run this path if it is valid there will be no error. –  Jun 16 '17 at 07:55
  • @PatrickHofman The file paths are being emitted by `Directory.GetFiles()`, so it appears they *do* exist. I'd wager it's a permissions problem. – Rob Jun 16 '17 at 07:56
  • The files are in my bin/Debug directory. The files are being found with Directory.GetFiles() but aren't being recognized. – blagh Jun 16 '17 at 07:56
  • @TAHASULTANTEMURI The path is *clearly* valid because `Directory.GetFiles()` is returning files. – Rob Jun 16 '17 at 07:56
  • @Rob thank you. I don't know why I got -1 for asking. The files are being found but also aren't. It's a difficult question to ask and I tested long before posting the question. I just tried running visual studio as administrator but that didn't seem to work. Any other suggestions? – blagh Jun 16 '17 at 07:58
  • You really need to show more. Where are the files located? What are the permissions. From where that code is running? – Patrick Hofman Jun 16 '17 at 07:59
  • @Rob may be right, from this link [Why does System.IO.File.Exists(string path) return false?](https://stackoverflow.com/a/18266662/6741868), the answer below it suggests trying `Console.WriteLine("File exists? " + File.Exists(Server.MapPath(string path)));` – Keyur PATEL Jun 16 '17 at 08:02
  • Try run program as Admin. –  Jun 16 '17 at 08:04
  • @PatrickHofman I added a screenshot of the folder but I'm not sure if that helps. The 2 files are there. They are in the bin/Debug folder nested a few folders deep. – blagh Jun 16 '17 at 08:05
  • In cases of file system (or registry) not doing what I expect I turn to [Process Monitor](https://technet.microsoft.com/en-gb/sysinternals/processmonitor) to see the details of the underlying Win32 API call. – Richard Jun 16 '17 at 08:06
  • @TAHASULTANTEMURI just tried running the .exe in the bin/Debug folder as admin but it still returns false. – blagh Jun 16 '17 at 08:09
  • I recently installed MalwareBytes on this computer a few days ago. Could that be the culprit? I just closed it and File.Exists() is still returning false. – blagh Jun 16 '17 at 08:10
  • See this question for possible causes: https://stackoverflow.com/questions/18266637/why-does-system-io-file-existsstring-path-return-false – Abbas Jun 16 '17 at 08:20
  • Can you please show the path of file inside foreach loop ? –  Jun 16 '17 at 08:22
  • First insert this code string extension = Path.GetExtension(fName); inside foreach loop then test the output in extension.I think the file extension is not recognizable. –  Jun 16 '17 at 08:28
  • @TAHASULTANTEMURI the relative path is in there already. Environment.CurrentDirectory returns "E:\Documents\Visual Studio Projects\MyProject\MyProject\bin\Debug" – blagh Jun 16 '17 at 08:30
  • @TAHASULTANTEMURI Please stop spamming this post with unrelated remarks. They are not helpful. – Patrick Hofman Jun 16 '17 at 08:30
  • @TAHASULTANTEMURI Path.GetExtension() returned the extensions correctly. Something is really wrong here... – blagh Jun 16 '17 at 08:34
  • @Abbas That link solved the problem. Opening it as a FileStream gave me a more detailed error. Surely enough: System.IO.PathTooLongException was thrown. I'm going to post the answer now. Thanks so much everyone! – blagh Jun 16 '17 at 08:43
  • 1
    it seems like the path is very long, could it be that it exceeds the allowed 255 characters? – Thomas Flinkow Jun 16 '17 at 08:47
  • @ThFl that was exactly the problem. File.Exists() simply returns false and doesn't throw an exception if the path is too long. – blagh Jun 16 '17 at 08:53
  • @blagh alright, I wasn't sure whether the solution could be this simple because I thought it would definitely throw an exception if so. Glad to know your problem is fixed however. – Thomas Flinkow Jun 16 '17 at 08:55

1 Answers1

2

This was sorted out in the comments of the question. Opening the file using a FileStream threw a 'System.IO.PathTooLongException' exception. File.Exists() simply returns false if it encounters any errors such as file path being too long.

@Abbas provided this link that fixed the issue and may be helpful: Why does System.IO.File.Exists(string path) return false?

Thanks everyone!

blagh
  • 71
  • 1
  • 1
  • 10