Save a file to disk from a document library

by Bas Hammendorp 13. March 2010 07:17

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!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

C# | Sharepoint

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About the author

My name is Bas Hammendorp, I work as an Enterprise Engineer at PGGM. My specialties are ASP.NET, C#, SQL and Sharepoint 2007.

Google ads