2012-01-05 488 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