static void Main(string[] args) { // replace this string with your // Sharepoint content DB connection string string DBConnString = "Server=SP2010\\gyccp;" + "Database=WSS_Content;Trusted_Connection=True;"; // create a DB connection SqlConnection con = new SqlConnection(DBConnString); con.Open(); // the query to grab all the files. SqlCommand com = con.CreateCommand(); com.CommandText = "SELECT ad.SiteId, ad.Id, ad.DirName," + " ad.LeafName, ads.Content" + " FROM AllDocs ad, AllDocStreams ads" + " WHERE ad.SiteId = ads.SiteId" + " AND ad.Id = ads.Id" + " AND ads.Content IS NOT NULL" + " Order by DirName"; // execute query SqlDataReader reader = com.ExecuteReader(); while (reader.Read()) { // grab the file’s directory and name string DirName = (string)reader["DirName"]; string LeafName = (string)reader["LeafName"]; Console.WriteLine("DirName:"+DirName); // create directory for the file if it doesn’t yet exist if (!Directory.Exists(DirName)) { Directory.CreateDirectory(DirName); Console.WriteLine("Creating directory: " + DirName); } // create a filestream to spit out the file Console.WriteLine("full path:" + DirName + "/" + LeafName); FileStream fs = new FileStream(DirName + "/" + LeafName, FileMode.Create, FileAccess.Write); BinaryWriter writer = new BinaryWriter(fs); // depending on the speed of your network, // you may want to change the buffer size (it’s in bytes) int bufferSize = 1000000; long startIndex = 0; long retval = 0; byte[] outByte = new byte[bufferSize]; // grab the file out of the db one chunk // (of size bufferSize) at a time do { retval = reader.GetBytes(4, startIndex, outByte, 0, bufferSize); startIndex += bufferSize; writer.Write(outByte, 0, (int)retval); writer.Flush(); } while (retval == bufferSize); // finish writing the file writer.Close(); fs.Close(); Console.WriteLine("Finished writing file: " + LeafName); } // close the DB connection and whatnots reader.Close(); con.Close(); Console.ReadKey(); }
The Code is as follows:
Protected static byte [] getbyte (string strfilepath) {// read the file filestream fsmyfile = new filestream (strfilepath, filemode. open, fileaccess. readwrite); // create a binary data stream reader and associate it with the opened file binaryreader brmyfile = new binaryreader (fsmyfile); // locate the file pointer to the start of brmyfile. basestream. seek (0, seekorigin. begin); byte [] bytes = brmyfile. readbytes (convert. toint32 (fsmyfile. length. tostring (); // close each of the above new objects brmyfile. close (); Return bytes ;}