C # for ASP.NET MVC FileStream Crossover

I wrote the code in C # and printed the results on the terminal to confirm that it works. Currently, I am moving part of the code to the MVC 4 controller, and I have been able to consistently combine most of it, but I am having problems with one part.

I want to read the database file (database.dat), and later I want to write to the same file.

In my controller, I:

using (FileStream stream = File.OpenRead ("database.dat")) database = (List) formatter.Deserialize (stream);

and

using (Stream stream = File.Open ("database.dat", FileMode.Create)) formatter.Serialize (stream, database);

In both cases, the β€œFile” in File.OpenRead and File.Open is underlined, and I get an error:

'System.Web.Mvc.Controller.File (byte [], string)' is a method that is not valid in this context ... "

Is there a way to achieve the same result in MVC?

+5
source share
1 answer

You will need to add the full name if you want to use the class Filein System.IO( http://msdn.microsoft.com/en-us/library/system.io.file.aspx ). So something like this should work:

using (FileStream stream = System.IO.File.OpenRead("database.dat")){
    database = (List)formatter.Deserialize(stream);
}
+9
source

All Articles