簡體   English   中英

如果共享點文檔庫中不存在目錄和子目錄,如何創建

[英]How to create directory and subdirectory if it does not exist in sharepoint Document Library

根據用戶輸入,我有一個用戶輸入為D:\\Test1\\Test2\\Test3\\Test4\\a\\b\\c\\d\\file.jpg我需要檢查文檔庫中是否存在文件夾和子文件夾。

DocLib >> Test1 >> Test2 .... di要復制文檔庫中的文件夾結構,如果存在,則直接讀取並保存文件,否則創建目錄,然后創建子目錄,直至保存級別為wherin的文件。

誰能幫助我了解我該如何處理? 我嘗試在硬盤上的本地系統中創建文件

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中的子目錄。 即通過保留指定的文件夾結構來添加文件。 請幫助

為此,您將需要一個一個地創建樹路徑的結構:這是一個簡短的代碼,說明如何使用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端創建與用戶傳遞的路徑中指定的文件夾相同的文件夾結構。

之后,您需要將文件保存在指定的文件夾中。

希望能幫助到你,

安德魯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM