Q:

Download File by Name from Document Library Programatically

Using SharePoint Server object model how can you retrieve a specific document by name in a Document Library, so that the file can be downloaded programatically?
I am finding a lot of articles with examples of iterating through a list or document library.  I would like to be able to retrieve a specific file by name, so that I can get the bytes or stream.  Is this possible?
Thanks for any tips or advise.

A:

You can use SPWeb.GetFile() method to retreive the document by its name.
Here's the example part of the code which can be used to solve your problem:
string s = "http://sp/Library/filename.png";
var f = web.GetFile(s);
if (f.Exists)
{
    var b = f.OpenBinary();
    System.IO.File.WriteAllBytes("C:\\temp\\fileName.png", b);
}

The web variable is the SPWeb that contains your library. You should create that object yourself or use the one from SPContext. You should also change the url of the file to match your file (variable s in my example).

