我创建了一个应用程序,该应用程序下载了SP网站中的所有文档库,但有一次它给了我这个错误(我尝试查看Google,但找不到任何东西,现在,如果有人知道解决此问题的任何方法,请回复,否则,谢谢看看)
System.IO.PathTooLongException:指定的路径,文件名或两者都太长。完全限定的文件名必须少于260个字符,目录名称必须少于248个字符。在System.IO.Path.NormalizePathFast(字符串路径,布尔fullCheck)在System.IO.Path.GetFullPathInternal(字符串路径)在System.IO.FileStream.Init(字符串路径,FileMode模式,FileAccess访问,Int32权限,布尔useRights ,FileShare共享,Int32 bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs,System.IO.FileStream..ctor(字符串路径,FileMode模式,FileAccess访问,FileShare共享,Int32 bufferSize,FileOptions选项)在System.IO.FileStream..ctor中。 IO.File.Create(字符串路径)
它达到了字符串的限制,代码如下所示,
#region Downloading Schemes private void btnDownload_Click(object sender, EventArgs e) { TreeNode currentNode = tvWebs.SelectedNode; SPObjectData objectData = (SPObjectData)currentNode.Tag; try { CreateLoggingFile(); using (SPWeb TopLevelWeb = objectData.Web) { if(TopLevelWeb != null) dwnEachWeb(TopLevelWeb, TopLevelWeb.Title, tbDirectory.Text); } } catch (Exception ex) { Trace.WriteLine(string.Format("Exception caught when tried to pass TopLevelWeb:{1}, Title = {2}, object data to (dwnEachWeb_method), Exception: {0}", ex.ToString(), objectData.Web, objectData.Title)); } finally { CloseLoggingFile(); } } private void dwnEachWeb(SPWeb TopLevelWeb, string FolderName, string CurrentDirectory) { if (TopLevelWeb != null) { if (TopLevelWeb.Webs != null) { CurrentDirectory = CurrentDirectory + "\\" + TopLevelWeb.Title; CreateFolder(CurrentDirectory); foreach (SPWeb ChildWeb in TopLevelWeb.Webs) { dwnEachWeb(ChildWeb, ChildWeb.Title, CurrentDirectory); ChildWeb.Dispose(); } dwnEachList(TopLevelWeb, CurrentDirectory); //dwnEachList(TopLevelWeb, FolderName, CurrentDirectory); } } } private void dwnEachList(SPWeb oWeb, string CurrentDirectory) { foreach (SPList oList in oWeb.Lists) { if (oList is SPDocumentLibrary && !oList.Hidden) { dwnEachFile(oList.RootFolder, CurrentDirectory); } } } private void dwnEachFile(SPFolder oFolder, string CurrentDirectory) { if (oFolder.Files.Count != 0) { CurrentDirectory = CurrentDirectory + "\\" + oFolder.Name; CreateFolder(CurrentDirectory); foreach (SPFile ofile in oFolder.Files) { if (CreateDirectoryStructure(CurrentDirectory, ofile.Url)) { var filepath = System.IO.Path.Combine(CurrentDirectory, ofile.Url); byte[] binFile = ofile.OpenBinary(); System.IO.FileStream fstream = System.IO.File.Create(filepath); fstream.Write(binFile, 0, binFile.Length); fstream.Close(); } } } } //creating directory where files will be download private bool CreateDirectoryStructure(string baseFolder, string filepath) { if (!Directory.Exists(baseFolder)) return false; var paths = filepath.Split('/'); for (var i = 0; i < paths.Length - 1; i++) { baseFolder = System.IO.Path.Combine(baseFolder, paths[i]); Directory.CreateDirectory(baseFolder); } return true; } //creating folders private bool CreateFolder(string CurrentDirectory) { if (!Directory.Exists(CurrentDirectory)) { Directory.CreateDirectory(CurrentDirectory); } return true; } //shorting string #endregion
由于错误的原因很明显,因此以下一些信息应可帮助您解决问题:
请参阅有关命名文件,路径和命名空间的这篇MS文章
这是链接的引文:
最大路径长度限制 在Windows API(以下段落中讨论的某些例外情况)中,路径的最大长度为MAX_PATH,它定义为260个字符。本地路径按以下顺序构造:驱动器号,冒号,反斜杠,用反斜杠分隔的名称组件以及终止的空字符。例如,驱动器D上的最大路径是“ D:\一些256个字符的字符串”,其中“ ”代表当前系统代码页的不可见终止空字符。(此处使用字符<>是为了清晰起见,并且不能成为有效路径字符串的一部分。)
和一些解决方法(摘自评论):
有解决各种问题的方法。下面列出的解决方案的基本思想始终是相同的:减小路径长度以具有path-length + name-length < MAX_PATH。你可以:
path-length + name-length < MAX_PATH