2012-01-05 1516 views
61

我创建了一个应用程序,可以下载SP站点中的所有文档库,但有一次它给了我这个错误(我试着看过谷歌,但不能找到任何东西,现在如果任何人都知道任何伎俩来解决这个问题,请回复否则谢谢你看它)解决文件路径太长的最佳方法例外

System.IO.PathTooLongException:指定的路径,文件名或这两者太长。完全限定的文件名必须少于260个字符,且目录名称必须少于248个字符。 (字符串路径,布尔fullCheck) 在系统.IO.Path.GetFullPathInternal(字符串路径) 在系统.IO.FileStream.Init ,布尔useRights,FileShare共享,Int32 bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs,字符串msgPath,布尔bFromProxy) 在System.IO.FileStream..ctor(字符串路径,FileMode模式,FileAccess访问,FileShare共享,Int32 bufferSize,FileOptions选项) 在System.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 
+1

转换的UNC(或其他)路径为8.3格式。 [转换为使用CMD 8.3格式] [1] [1]:http://stackoverflow.com/questions/10227144/convert-long-filename-to-short-filename-8 -3-using-cmd-exe – AutomationNation 2014-04-07 16:47:35

+0

[如何避免System.IO.PathTooLongException?](http://stackoverflow.com/questions/530109/how-to-avoid-system-io-pathtoolongexception) – Deantwo 2017-03-14 12:41:17

回答

46

由于错误的原因是显而易见的,这里的一些信息,希望能够帮助您解决问题:

看到这个MS article about Naming Files, Paths, and Namespaces

下面是来自链接报价:

最大路径长度限制在Windows API(以下段落中讨论的一些例外)中,路径的最大长度 是MAX_PATH,它被定义为260 charac TER值。本地 路径按以下顺序组织:驱动器号,冒号, 反斜杠,用反斜杠分隔的名称组件以及终止的 空字符。例如,驱动器D上的最大路径为“D:\ some 256个字符的路径字符串<NUL>”其中“<NUL>”表示终止当前系统代码页的空字符的不可见 。 (该 字符< >这里用于视觉清晰度,并不能成为 一个有效的路径字符串的一部分。)

和几个解决方法(从注释中获取):

有办法解决各种问题。下面列出的解决方案的基本思想总是相同的:缩小路径长度以便拥有path-length + name-length < MAX_PATH。您可以:

  • 分享的子目录下,
  • 使用命令行通过SUBST
  • 的手段来分配驱动器号
  • VB下使用AddConnection到一个驱动器号分配给路径
+7

@ TimeToThine,你读过我发布的文章吗?你读过评论吗?我可能是错的,但我不认为你会从SO社区获得更多帮助,除了我已经提供的。 – 2012-01-05 16:51:09

+2

是的,我已经读过,在发布我的问题之前,我甚至尝试过“\\?\”,但由于某种原因,它在这方面不起作用。我发现这个博客,使用它,但由于某种原因,它不能正常工作, “http://www.codinghorror.com/blog/2006/08/shortening-long-file-paths.html” 我仍然在寻找的东西保持目录保存,我可以从那里,或类似的东西,例如使用隐藏标签来保存当前目录而不是字符串,但不知道它是否会工作。 – 2012-01-05 17:10:26

+4

这很明显,但没有任何意义。为什么会有路径大小限制?它是2017. – Jaider 2017-01-18 19:04:47

2

在Windows 8.1,使用。 NET 3.5,我有一个类似的问题。
虽然我的文件名只有239个字符长度,但当我去实例化一个FileInfo对象时,只有文件名(没有路径)发生了System类型的异常。 IO.PathTooLongException

2014-01-22 11:10:35 DEBUG LogicalDOCOutlookAddIn.LogicalDOCAddIn - fileName.Length: 239 
2014-01-22 11:10:35 ERROR LogicalDOCOutlookAddIn.LogicalDOCAddIn - Exception in ImportEmail System.IO.PathTooLongException: Percorso e/o nome di file specificato troppo lungo. Il nome di file completo deve contenere meno di 260 caratteri, mentre il nome di directory deve contenere meno di 248 caratteri. 
    in System.IO.Path.NormalizePathFast(String path, Boolean fullCheck) 
    in System.IO.FileInfo..ctor(String fileName) 
    in LogicalDOCOutlookAddIn.LogicalDOCAddIn.GetTempFilePath(String fileName) in C:\Users\alle\Documents\Visual Studio 2010\Projects\MyAddin1Outlook20072010\MyAddin1Outlook20072010\LogicalDOCAddIn.cs:riga 692 
    in LogicalDOCOutlookAddIn.LogicalDOCAddIn.ImportEmail(_MailItem mailItem, OutlookConfigXML configXML, Int64 targetFolderID, String SID) in C:\Users\alle\Documents\Visual Studio 2010\Projects\MyAddin1Outlook20072010\MyAddin1Outlook20072010\LogicalDOCAddIn.cs:riga 857 
    in LogicalDOCOutlookAddIn.LogicalDOCAddIn.ImportEmails(Explorers explorers, OutlookConfigXML configXML, Int64 targetFolderID, Boolean suppressResultMB) in C:\Users\alle\Documents\Visual Studio 2010\Projects\MyAddin1Outlook20072010\MyAddin1Outlook20072010\LogicalDOCAddIn.cs:riga 99 

我解决了问题微调的文件名字符204(包括扩展名)。

0

您可以使用较短的目录创建符号链接。 第一个打开命令行例如通过Shift + RightClick在您想要的文件夹中使用较短的路径(您可能必须以管理员身份运行它)。

然后用相对或绝对的路径类型:

mklink ShortPath\To\YourLinkedSolution C:\Path\To\Your\Solution /D 

然后开始从较短的路径的解。这里的优点是:您不必移动任何东西。

-3

可以在降低工程名称:

Solution Properties -> Application -> Assembly Name