Why can't I use System.IO.File methods in an MVC controller?

C#asp.net MvcFile

C# Problem Overview


I am trying to see if a file exists before using it in an MVC controller:

string path = "content/image.jpg";

if (File.Exists(path))
{ 
    //Other code
}

The File keyword is underlined in red, and the compiler shows an error:

> System.Web.MVC.Controller.File(string, string, string) is a > 'method', witch is not valid in the given context.

How can I use File.Exists() in a controller?

C# Solutions


Solution 1 - C#

You should prefix it with a namespace:

if (System.IO.File.Exists(picPath))
{ 
    //Other code
}

The reason for that is because you are writing this code inside a controller action which already defines a File method on the Controller class.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPomsterView Question on Stackoverflow
Solution 1 - C#Darin DimitrovView Answer on Stackoverflow