• ImportBraces (unregistered)

    File.WriteAllText(strPath, "Frist!");

  • Best Of 2021 (unregistered)

    Lol ok this is great, a true WTF

  • Chris (unregistered)

    The fact that they've implemented a method called FileExists, when File.Exists(String) already exists, tells you it's going to be bad. It even returns false if the caller doesn't have permissions to read the file, just like this method.

    I'm guessing what they want to know is whether or not there is a file they can overwrite there, or if they can add a file there if it doesn't exist, and make that a one-liner. But then you have File.OpenWrite which tells you that, except it tells you when you can't by means of exceptions.

  • (nodebb)

    JFC now that's a WTF. Big yikes.

  • (nodebb)

    I'm trying to imagine why File.WriteAllText itself was even included in the .net APIs.

  • my name is missing (unregistered)

    Should be called FileExisted.

  • (nodebb) in reply to Steve_The_Cynic

    Why not? They have many helper methods which are shortcuts for a series of boiler plate steps. It increases productivity.

  • (nodebb) in reply to my name is missing

    No, it should've been called DoNotCallAndDeleteMe.

  • Sole Purpose Of Visit (unregistered) in reply to Mr. TA

    No, it should be named CallAndDeleteMe. And it should return a phone number, just so you know who to delete.

  • Ollie Jones (unregistered)

    Classic tri-state Boolean, revisited.

    True / False / File Destroyed

  • Duston (unregistered)

    Oh, come on, it's not that WTF-y, it only returns true or false and not FILE_NOT_FOUND.

  • Fant (unregistered)

    It even returns true if the file does not exist, because File.WriteAllText creates the file for you if it is not already there …

  • LCrawford (unregistered)

    if (!FileExists(companyDBPath)) { ' File existed, sharing violation } else { ' File existed, past tense }

  • Jay (unregistered)

    This expects that any file you give it is new and not containing anything important. That's the ONLY reason I can think of using this method, and even then, you'd be better off with a method that creates a new file instead and returns the name.

    I would burn this piece of code or at least rename it and HEAVILY restrict it's invocation to ONLY safe cases.

  • Joshua (unregistered) in reply to Chris

    File.Exists is broken though. Though someone who manages to find that isn't writing this horror.

  • King (unregistered)

    Should be named EnsureFileExists.

  • Gummy Gus (unregistered)

    .. and let's not forget that it can also succeed for any named thing that can be opened, like a device, or scanner, or printer or tty or /proc and who knows that else.

  • Prime Mover (unregistered)

    This method was written by the same guy who wrote the method "MemoryAvailable" which deletes everything off your hard drive and returns how much data it can hold.

  • Staticman (unregistered)

    Even more disappointing was the placeholder text, "blah", missing an opportunity to do something cute like "I love cats".

  • Bobby (unregistered) in reply to my name is missing

    😂

  • Sole Purpose Of Visit (unregistered) in reply to Gummy Gus

    Well, it would do if it wasn't C#. And if it had access to "/proc" or anything else that could be "opened." And if the File God Object was able to open something on a *nix system, sitting under "/proc" or wherever.

    But none of that is true, so you've just managed to inject a whole new WTF on top of something that is already a really classy WTF of its own.

  • Angela Anuszewski (google) in reply to my name is missing

    If this was Reddit, I would have actually just spent money to award you.

  • (nodebb)

    While the name is a true WTF, the purpose of this function seems pretty clear to me. It's intended to be used before you open a file for writing, to ensure that the file exists and is writable. I guess they didn't want to deal with exception handling, so this turns the exception into a boolean.

  • Lurch (unregistered)

    It should be called "CanOverwriteFile" -- then you're giving permission for what it tries to do.

  • COB (unregistered) in reply to my name is missing

    OK, this is the funniest comment I've seen in a while!

  • holy shit now I'm actually commenting here... (unregistered) in reply to Sole Purpose Of Visit

    Well, it would do if it wasn't C#. And if it had access to "/proc" or anything else that could be "opened." And if the File God Object was able to open something on a *nix system, sitting under "/proc" or wherever.

    Not that far fetched actually, there is Mono, you know?

  • (nodebb) in reply to King

    This is not touch()! That makes sure the file exists; if it is there, update the read timestamp. If it is not there create an empty file. In no case does touch overwrite an existing file.

  • (nodebb)

    I am wondering if this was left as a time-bomb by a disgruntled employee just before he quit. Time to check the source control and see how many important files just say "blah".

  • (nodebb) in reply to holy shit now I'm actually commenting here...

    Or .NET Core, which runs quite nicely on Linux (and has far better performance and reliability than Mono, at least from my experiences involving running ASP.NET applications).

  • shcode (unregistered) in reply to my name is missing

    if(FileExisted()) bool fuckThisCodebase = true;

  • Charles (unregistered) in reply to Barry Margolin 0

    It would still be wrong, with a subtle TOCTOU bug - it might find the file could be created, but then when you try creating the real file with the same name something has happened to prevent it and you're left with the file with junk in it instead of the proper contents.

  • gone ratting (unregistered)

    Anyone remember the opening voice intro to Hexen? That's what I hear when I subvocalize this method's name. Like spell-casting by a Ring Wraith that got queer-eyed.

  • (nodebb)

    I can't tell you how much this one made me laugh today. :)

  • (nodebb) in reply to holy shit now I'm actually commenting here...

    Not that far fetched actually, there is Mono, you know?

    Not to mention .Net Core! (which is what all .Net will be as of 5.0)

  • (nodebb)

    At first look, I thought it's from my famous cow-orker Kevin. But it isn't - Kevin would add a LastError = ex.Message; or something like that in the Exception handler.

Leave a comment on “The Threat of Existence”

Log In or post as a guest

Replying to comment #:

« Return to Article