Verify if file exists or not in C#

后端 未结 12 1342
名媛妹妹
名媛妹妹 2020-12-15 17:19

I am working on an application. That application should get the resume from the users, so that I need a code to verify whether a file exists or not.

I\'m using ASP.N

12条回答
  •  时光取名叫无心
    2020-12-15 17:41

    Can't comment yet, but I just wanted to disagree/clarify with erikkallen.

    You should not just catch the exception in the situation you've described. If you KNEW that the file should be there and due to some exceptional case, it wasn't, then it would be acceptable to just attempt to access the file and catch any exception that occurs.

    In this case, however, you are receiving input from a user and have little reason to believe that the file exists. Here you should always use File.Exists().

    I know it is cliché, but you should only use Exceptions for an exceptional event, not as part as the normal flow of your application. It is expensive and makes code more difficult to read/follow.

提交回复
热议问题