我被要求使用c#工具将本地1TB文件夹的数据复制到Laserfiche存储库中。我通常用这个方法来复制一个文件夹树。
我正在使用这种方法枚举本地文件夹,以便只获得文件夹及其树。
IEnumerable<string> AllFolders2(string root)
{
var folders = Directory.EnumerateDirectories(root, "*", SearchOption.AllDirectories).Select(path => path.Replace(root, ""));
var listOfFiles = folders.ToList();
return listOfFiles;
}
我的问题是我被允许使用的Laserfiche SDK。链接方法使用的是Directory.CreateDirectory(dirPath.Replace(sourcePath, targetPath));
但SDK给我的是
方法void Create(string Name, LFFolder ParentFolder, bool AutoRename);
需要父文件夹。
void CreateFolder(string FolderName, string ParentPath)
{
try
{
lfParentFolder = (LFFolder)lfDB.GetEntryByPath(ParentPath);
lfFolder = new LFFolder();
try
{
lfFolder = (LFFolder)lfDB.GetEntryByPath(string.Format(@"{0}\{1}", lfParentFolder.FullPath, FolderName));
}
catch (COMException ex)
{
if (ex.ErrorCode == -1073470679)
{
lfFolder.Create(FolderName, lfParentFolder, false);
}
}
}
catch (Exception ex)
{
throw ex;
}
}
我被困在获得更深层次的父文件夹。SDK是8.1,有什么想法吗?
您需要递归地遍历每个目录,而不是使用SearchOption.AllDirectories。下面是递归遍历文件夹的示例。该代码将一个文件夹(及其子文件夹)中的所有文件打包成一个XML文件。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace SAveDirectoriesXml
{
class Program
{
const string FILENAME = @"c:temptest.xml";
const string FOLDER = @"c:temp";
static XmlWriter writer = null;
static void Main(string[] args)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
writer = XmlWriter.Create(FILENAME, settings);
writer.WriteStartDocument(true);
DirectoryInfo info = new DirectoryInfo(FOLDER);
WriteTree(info);
writer.WriteEndDocument();
writer.Flush();
writer.Close();
Console.WriteLine("Enter Return");
Console.ReadLine();
}
static long WriteTree(DirectoryInfo info)
{
long size = 0;
writer.WriteStartElement("Folder");
try
{
writer.WriteAttributeString("name", info.Name);
writer.WriteAttributeString("numberSubFolders", info.GetDirectories().Count().ToString());
writer.WriteAttributeString("numberFiles", info.GetFiles().Count().ToString());
writer.WriteAttributeString("date", info.LastWriteTime.ToString());
foreach (DirectoryInfo childInfo in info.GetDirectories())
{
size += WriteTree(childInfo);
}
}
catch (Exception ex)
{
string errorMsg = string.Format("Exception Folder : {0}, Error : {1}", info.FullName, ex.Message);
Console.WriteLine(errorMsg);
writer.WriteElementString("Error", errorMsg);
}
FileInfo[] fileInfo = null;
try
{
fileInfo = info.GetFiles();
}
catch (Exception ex)
{
string errorMsg = string.Format("Exception FileInfo : {0}, Error : {1}", info.FullName, ex.Message);
Console.WriteLine(errorMsg);
writer.WriteElementString("Error",errorMsg);
}
if (fileInfo != null)
{
foreach (FileInfo finfo in fileInfo)
{
try
{
writer.WriteStartElement("File");
writer.WriteAttributeString("name", finfo.Name);
writer.WriteAttributeString("size", finfo.Length.ToString());
writer.WriteAttributeString("date", info.LastWriteTime.ToString());
writer.WriteEndElement();
size += finfo.Length;
}
catch (Exception ex)
{
string errorMsg = string.Format("Exception File : {0}, Error : {1}", finfo.FullName, ex.Message);
Console.WriteLine(errorMsg);
writer.WriteElementString("Error", errorMsg);
}
}
}
writer.WriteElementString("size", size.ToString());
writer.WriteEndElement();
return size;
}
}
}