SharePoint downloads all files under the port from the database

Source: Internet
Author: User
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 ;}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.