2013-02-22 149 views
1

我有一個用戶輸入爲D:\Test1\Test2\Test3\Test4\a\b\c\d\file.jpg根據用戶輸入我需要檢查文件夾和子文件夾是否存在於文檔庫中。如何創建目錄和子目錄,如果它不存在SharePoint文件庫

DocLib >> >>的Test1 Test2的....迪想要複製文檔庫中的文件夾結構,如果它存在,不是直接讀取和保存其他文件創建目錄和子目錄比高達並且在級別的文件應該保存。

任何人都可以幫助我理解我該如何去與此?我試着用硬盤驅動器上創建本地系統文件

static void CopyFolder(string sourceFolder, string destFolder) 
     { 
      if (!Directory.Exists(sourceFolder)) 
       Directory.CreateDirectory(destFolder); 
      string[] files = Directory.GetFiles(sourceFolder); 
      foreach (string file in files) 
      { 
       string name = Path.GetFileName(file); 
       string dest = Path.Combine(destFolder, name); 
       File.Copy(file, dest); 
      } 

      //check folder in the source destination 
      string[] folders = Directory.GetDirectories(sourceFolder); 
      foreach (string folder in folders) 
      { 
       string name = Path.GetFileName(folder); 
       string dest = Path.Combine(destFolder, name); 
       System.IO.Directory.CreateDirectory(dest); 
       CopyFolder(folder, dest); 
      } 
     } 

不知道如何檢查,如果目錄中存在比檢查SharePoint中的子目錄。即通過保留指定的文件夾結構來添加文件。好心幫

回答

1

要做到這一點,你會被一個需要樹路一網站製作結構:這裏是一個簡短的代碼如何將它的根站點與UserDocument中的文件夾的根文件夾上進行:

  // This will contain all information about the path 
      DirectoryInfo infoDir = new DirectoryInfo(@"C:\Users\Administrator\Pictures2\WallPaperHD - 078.jpg"); 

      // Root folder passed => Default in SharePoint 
      if (infoDir.Parent != null) 
      { 
       // All folders are stored here 
       List<string> folders = new List<string>(); 

       // Set current folder to parent 
       DirectoryInfo currentDir = infoDir.Parent; 
       do 
       { 
        // Add its name to array 
        folders.Add(currentDir.Name); 

        // Set parent of current as current if available 
        if (currentDir.Parent != null) 
         currentDir = currentDir.Parent; 
       } 
       while (currentDir.Parent != null); 

       // Add SP structure) 
       using (SPSite site = new SPSite("http://testsite.dev")) 
       { 
        SPWeb web = site.RootWeb; 
        // Get doc library 
        SPList documentLibrary = web.GetList("/UserDocuments"); 
        // If library root exists 
        if (documentLibrary != null) 
        { 
         string folderUrl = "/UserDocuments/"; 

         for (int i = folders.Count - 1; i >= 0; i--) 
         { 
          string folder = folders[i]; 
          SPFolder newFolder = site.RootWeb.GetFolder(folderUrl + folder); 
          if (!newFolder.Exists) 
          { 
           site.RootWeb.Folders.Add(folderUrl + folder); 
           // Save changes 
           site.RootWeb.Update(); 

           folderUrl += folder + "/"; 
          } 
         } 
        } 
       } 
      } 

這將在SharePoint端創建與用戶傳遞的路徑中指定的文件夾相同的文件夾結構。

完成這一切後,您需要的是將文件保存在指定的文件夾中。

希望它能幫助,

安德魯

相關問題