Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
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:
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!
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I'm currently writing a program and using my own file extension ".pn" for these files. When I use the BinaryWriter in FileMode.Create to write to the file "primes.pn" and then look in the file explorer it does not have the extension I asked. My file explorer has display file extensions turned on. How could I prevent this from happening/what is causing this?
Additionally the check for whether "primes.pn" exists passes but then when checking the length of the file it throws a System.IO.FileNotFoundException.
if (!File.Exists("res_divisibility/primes.pn") && bytes.Count > new FileInfo("res_divisibility/primes.pn").Length)
{
BinaryWriter bw = new BinaryWriter(new FileStream("res_divisibility/primes.pn", FileMode.Create));
foreach(byte b in bytes)
bw.Write(b);
}
Still not sure what was causing the problem but File.WriteAllBytes()solves this issue. The existence check was my mistake because I was using && instead of ||.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm working on a program which requires the lookup of files in user defined directories. If the files don't exist, it must continue to use it's own resources. The problem is, even though some files don't exist, the function File.Exists returns true for them.
Here is an image of the error:
Here is the code that looks for the file and produces the error:
string[] vmtLines = null;
if (File.Exists(vmtFile))
{
try { vmtLines = File.ReadAllLines(vmtFile); }
catch (Exception e) { Debug.Log(vmtFile + " Exists: " + File.Exists(vmtFile) + ", " + e.Message); }
}
It turns out the problem is not in C#, Unity, or my programming. It seems there's a problem with files that are called con. I was testing out in the cmd to see if I could open the file con.vmt with notepad, here's what happened:
Then I tried to see if the same thing happens with other files in the same directory:
It opened just fine. Then I tried to see if it were just 3 letter file names which was the problem by opening a file with the name com:
It didn't have some weird problem, then I tried to see if it was something with the extension vmt and the file name con:
It had the same problem. I tried the same thing in the parent directory, and the same problem occurred with the file name con. So I assume this happens anywhere.
Thank you #Programmer, #CharlesMager, #AlexK, #Quantic, #ScottChamberlain, and #LarsTech for trying to help. This was one strange problem
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm having an issue in saving an rtf file, I'm saving it in a subfolder in "MyDocuments" the strange this is that i also save a .bin file and some other stuff and it doesn't causes any exeption, i'am pc admin so why does it happen?
string docs = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Monitor\\Immobili\\";`
richTextBox1.SaveFile(docs+code);
code is also another string
An UnauthorizedAccessException means one of 3 things:
The caller does not have the required permission.
Path is a directory.
Path specified a read-only file.
Check for last two reasons. I think you are not adding filename after path. Add breakpoints and check if richTextBox1.SaveFile(docs+code); is getting the complete path to the file instead of just folder path.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
At the moment I'm currently trying to get all files from a specified directory. The files I'm looking for end with the file type .ship, however, my code returns "path\NameOfFiles.ship.ship" and I can't for the life of me decide where the extra file extension is added.
Here's the function I use
public static List<SaveEntry> GetAllSavesFromProfile(string ProfileName)
{
if (IsProfileMissing(ProfileName))
{
Debug.LogError("Couldn't find save from profile. Check if it exists before calling this function");
return new List<SaveEntry>();
}
List<SaveEntry> ListToReturn = new List<SaveEntry>();
List<string> SaveEntries = Directory.GetFiles(Application.persistentDataPath + "/Profile_" + ProfileName, "*", SearchOption.TopDirectoryOnly).Where(s => s.EndsWith(".ship", StringComparison.CurrentCultureIgnoreCase)).ToList<string>();
foreach (string entry in SaveEntries)
{
ListToReturn.Add(LoadShip(ProfileName, entry));
}
return ListToReturn;
}
I'm pretty I'm doing something wrong with the Where function, but I can't decide exactly where.
I suspect your filenames contains an extra .ship. You can verify that if you change your settings to show file extensions.
Also, you can use a search pattern instead of using Where
Directory.GetFiles(Application.persistentDataPath + "/Profile_" + ProfileName, "*.ship", SearchOption.TopDirectoryOnly)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hi
I am trying to read a plain text file named "test.txt" from my system but all the attempts
File.Exists(), StreamReder are not getting the file, that is not a new task for me but i am irritated due to this strange behavior. I have given full permissions to the file but in vain. I am doing the test in a C# console application. The system has a fresh installation and I am wondering of any permission issue when run in debug mode. I also copied the file to Debug folder but still same error. Can anyone please guide me about this? Thanks in advance
There is a neat function in C# for reading string files: (in System.IO namespace)
string text = File.ReadAllText("test.txt");
If you are having trouble with the path, you can add test.txt as a resource with copy to (add the file to teh project, right-click properties and select Copy to output directory.
Then you can use:
string path = Path.Combine(Directory.GetCurrentDirectory(), "test.txt");
File.ReadAllText(path);
Have you stepped through the code? The first step is to make sure the path being used in the program is correct.
Debug mode will still run under your account so, if you have permission to open the file, that won't be a problem.