In my new update for the WSP Listener I need to save a file from a document library to disk. This can be done by the following code:
// item is an SPListItem
SPFile myFile = item.File;
DestinationFile = Path.Combine(@"c:\temp", myFile.Name);
if (File.Exists(DestinationFile))
{
// Remove the old file
File.Delete(DestinationFile);
}
// Open the binary data
byte[] data = myFile.OpenBinary();
using (FileStream stream = new FileStream(DestinationFile, FileMode.CreateNew))
{
// Save the file
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(data, 0, (int)myFile.Length);
writer.Close();
stream.Close();
}
Have fun!