Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

File.Exists is not remotely working!!!

Discussion in 'Scripting' started by ackley14, Jul 15, 2015.

  1. ackley14

    ackley14

    Joined:
    Oct 31, 2014
    Posts:
    31
    Every single time i use File.Exists it simply REFUSES to be even 0.0000001% accurate....ok overstatement yes but my point is it ALWAYS returns false, with the simple exception of the time where i use a file path derived from a DirectoryInfo call. this is extremely frustrating due to the fact that yes, im using the absolute path, yes i even tried path.combine....but nothing....nothing works. this is so utterly aggrivating

    my current alternative is to have a sort of "manual" file.exists in the context of the script by which i would pull all files via DirectoryInfo, and compare them to the file im looking for untill it tells me it does or doesn't exist.

    this is very very frustrating and its making my save system significantly more complex then it needs to be.
    thank you so much to anybody who can help point me in the right direction towards using this CLEARLY broken function.

    and for posterity's sake:
    Code (JavaScript):
    1. print(File.Exists(Path.Combine(Application.dataPath ,String.Format("/../saves/{0}{1}.txt",cont.instance.Savedata[0].Name,count))));
    ^this returns false even tho im literally looking on my screen right now at the file. that was saved a min ago using the EXACT same path information...yet it does not "exist"..........

    when i print just the path without the "file.exists" portion, it comes up VERBATIM to what i see on my screen with the folder open. and no, File.OpenRead doesn't recognize it either, again however returning the path verbatim.

    my appolagies for the "flaming" but this issue has cropped up several times durring this phase of development and its giving me the biggest frustration headache ever lol. i see absolutely no reason this shouldn't work...and yet it doesn't

    if I'M doing something wrong...just let me down easy..XD again thanks for any and all help!
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Did you try to use Path.GetFullPath to eliminate the ".."? If I remember correctly, some operations don't like "..".
     
  3. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    Here are a few things you could try:

    Code (csharp):
    1. Debug.Log(Path.Combine(Application.dataPath ,String.Format("/../saves/{0}{1}.txt",cont.instance.Savedata[0].Name,count)));
    Verify the logged path is exactly what you expect

    You said you just wrote to this file. Make sure the file handle is closed. You should be able to open the file in another program if this is the case.

    Check the permissions on the file and directory. I'm not sure how this could happen but maybe Unity doesn't have Read or List folder contents access to this directory?
     
  4. ackley14

    ackley14

    Joined:
    Oct 31, 2014
    Posts:
    31
    THANK YOU!!!! this is perfect, thank you so much for your quick response!! you've really helped me out!
     
  5. ackley14

    ackley14

    Joined:
    Oct 31, 2014
    Posts:
    31
    thanks so much for the response! i should have worded that better lol. 99% of the time when i save a file , i immedietly stop the game to clear its references from memory so they don't interfer with tests (like file.exists)

    i didn't test this out but if this problem comes back up this is the first thing i'll try! thanks again!