Why can't I use System.IO.File methods in an MVC controller?
C#asp.net MvcFileC# 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.