다음을 통해 공유


Code snippet to download the files along with versions from SPS 2003 document library.

    1:          private void Download()
    2:          {
    3:              string  siteUrl     = txtSite.Text.ToString();
    4:              string  source      = txtDocLib.Text.ToString();
    5:              string  downloadPath= txtDownload.Text.ToString();
    6:              byte[]  vContent    = null;
    7:              int     count       = 1;    
    8:   
    9:              using (SPSite site = new SPSite(siteUrl))
   10:              {
   11:                  using (SPWeb web = site.OpenWeb())
   12:                  {
   13:                      SPFolder sourceFolder = web.GetFolder(source);
   14:                      
   15:                      //fetching data from doc lib
   16:                      foreach (SPFile file in sourceFolder.Files)
   17:                      {
   18:                          //saving the main file
   19:                          byte[] data= file.OpenBinary();
   20:                          SaveFile(downloadPath + "\\" + file.Name, data);
   21:                        
   22:                          //saving each file's versions now
   23:   
   24:                          //resetting the counter of file names
   25:                          count = 1;
   26:                          SPFileVersionCollection vCol = file.Versions;
   27:                          for (int i = vCol.Count - 1; i >= 0; i--)
   28:                          {
   29:                              vContent = vCol[i].OpenBinary();
   30:                              SaveFile(downloadPath + "\\" + file.Name.Substring(0,file.Name.IndexOf(".")) + "-version"
   31:                                        + count.ToString() + file.Name.Substring(file.Name.IndexOf(".")), vContent);
   32:                              count++;
   33:                          }
   34:                      }
   35:                  }
   36:              }
   37:   
   38:              MessageBox.Show("Done!");
   39:          }
   40:   
   41:          //to save file on the local file system
   42:          private void SaveFile(string fileName, byte[] data)
   43:          {
   44:              try
   45:              {
   46:                  System.IO.FileStream fileStream = new System.IO.FileStream(fileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
   47:                  fileStream.Write(data, 0, data.Length);
   48:                  fileStream.Close();
   49:              }
   50:              catch (Exception ex)
   51:              {
   52:                  MessageBox.Show("Exception : " + ex.ToString());
   53:              }
   54:          }

:: Please note that I would have just uploaded my initial code and you might want to consider proper optimization of the code and disposal of objects properly. I might not have updated the latest code here.