Sending A File As A Method Argument?

12,664

It depends what you need to do with it :)

Options include:

  • path (string)
  • contents (string or byte[])
  • some complex object model (od your own choosing) of the contents
  • FileInfo
  • FileStream (or just Stream)
  • some kind of reader/writer; TextReader, XmlWriter etc
Share:
12,664
sooprise
Author by

sooprise

I like to program, but am a noob, which is why I'm here. <-My sexy fatbooth picture

Updated on June 04, 2022

Comments

  • sooprise
    sooprise about 2 years

    Is this sort of thing possible:

    public static void DoThis(file aFile){
        //Blah blah blah
    }
    
  • Muad'Dib
    Muad'Dib over 13 years
    @Marc Gravell an instance of a class of the type "file"
  • Brian
    Brian over 13 years
    C# will allow you to pass a handle to a file using IntPtr or SafeFileHandle. However, that's a bit low level and mainly is only useful for interop. See stackoverflow.com/questions/3562513/… for discussion.
  • Brian
    Brian over 13 years
    @Muad'Dib: As file is not part of the framework, your answer strikes me as begging the question.
  • Muad'Dib
    Muad'Dib over 13 years
    @Brian the OP supplied the paramaters, and it was his/her code that i was using.
  • Brian
    Brian over 13 years
    @Muad'Dib: But since the OP was asking, "Is this sort of thing possible?" your answer doesn't provide any information on how to make it possible. As the title indicates, the OP's underlying question is how to send a file as a method argument. The code is an example of the syntax he hopes to use.
  • Muad'Dib
    Muad'Dib over 13 years
    @Brian and the OP doesn't say it is a System.IO.File or some custom class, he just asks if it is possible. and, as we know, it is.
  • BornToCode
    BornToCode over 10 years
    If it's a posted file (from HTML input field) you could also pass the file as a HttpPostedFile (its class is in System.Web.dll)
  • floydheld
    floydheld almost 3 years
    maybe a short example for each option?