[英]How to create directory and subdirectory if it does not exist in sharepoint Document Library
I have a user input as D:\\Test1\\Test2\\Test3\\Test4\\a\\b\\c\\d\\file.jpg
as per the user input i need to check if folder and sub folder exist in a Document Library. 根据用户输入,我有一个用户输入为D:\\Test1\\Test2\\Test3\\Test4\\a\\b\\c\\d\\file.jpg
我需要检查文档库中是否存在文件夹和子文件夹。
ie 即
DocLib>>Test1>>Test2....di want to replicate the folder structure in Document Library, if it exist than directly read and save the file else create directory and than subdirectory and upto the level wherin file should be saved. DocLib >> Test1 >> Test2 .... di要复制文档库中的文件夹结构,如果存在,则直接读取并保存文件,否则创建目录,然后创建子目录,直至保存级别为wherin的文件。
Can anyone help me to understand how can i go with this? 谁能帮助我了解我该如何处理? I tried with creating files in local system on hard drive 我尝试在硬盘上的本地系统中创建文件
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);
}
}
No idea how to check if directory exist and than check for subdirectory in sharepoint. 不知道如何检查目录是否存在,而不是如何检查sharepoint中的子目录。 ie add a file by retaining the folder structure specified. 即通过保留指定的文件夹结构来添加文件。 Kindly help 请帮助
To do this you will need to createthe structure of the tree path one by one: here is a short code how it can be done on the root site with UserDocument folder as a root folder: 为此,您将需要一个一个地创建树路径的结构:这是一个简短的代码,说明如何使用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 + "/";
}
}
}
}
}
This will create the same structure of folders on the SharePoint side as it was specified in the path passed by user. 这将在SharePoint端创建与用户传递的路径中指定的文件夹相同的文件夹结构。
After this all you need is to save file in the specified folder. 之后,您需要将文件保存在指定的文件夹中。
Hope it helps, 希望能帮助到你,
Andrew 安德鲁
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.