Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use File.WriteAllBytes() in asp.net MVC, cannot choose method from method group did you intend to invoke the method? [duplicate]

I'm trying to create a copy of a file i have in my database as a byte array, I'm looking for the easiest way to do this, and on SO and everywhere else the answer seems to be:

File.WriteAllBytes(string newfilepath, byte[] theFile);

However I'm unable to use pretty much anything from the File class, and as soon as I type in File. intellisense draws a red line underneath it and says:

cannot choose method from method group did you intend to invoke the method?

I realise this is a very generic question, and the solution is most probably a very simple one, but I can't figure it out for the life of me. Any ideas?

And here is the code where I try to create the file:

    var labProcessOrderLetter = from obj1 in context.LabArticles
                                join obj2 in context.ProcessLabs
                                on obj1.ProcessForLabID equals obj2.ID
                                where obj1.ID == _articleId
                                select obj2.LetterAttachment;

    byte[] thePDFLetter = (byte[]) labProcessOrderLetter.FirstOrDefault();

    var uploadPath = Server.MapPath("~/_TEMP/PDF");
    var tempfilename = Guid.NewGuid().ToString();
    var tempfilenameandlocation = Path.Combine(uploadPath, Path.GetFileName(tempfilename));

    File.WriteAllBytes( tempfilenameandlocation, thePDFLetter);

EDIT: Obviously I have already imported the System.IO namespace, and nowhere in my project I have a method or class called File, or even beginning with File.

like image 953
Mohammad Sepahvand Avatar asked Dec 19 '12 08:12

Mohammad Sepahvand


2 Answers

It sounds like you have a method that's visible to you (in the same class, say) called File. This will be chosen in preference to System.IO.File. So you need to fully qualify the name:

System.IO.File.WriteAllBytes(tempfilenameandlocation, thePDFLetter);

(In fact, enough clues were in the question. It's Controller.File that's in scope)

like image 168
Damien_The_Unbeliever Avatar answered Oct 07 '22 11:10

Damien_The_Unbeliever


A controller has a File already defined, for outputting files in a nice way. Just fully qualify it with System.IO.File

like image 20
Dave Avatar answered Oct 07 '22 11:10

Dave