C# 解决文件路径过长异常的最佳方法

C# 解决文件路径过长异常的最佳方法,c#,.net,string,file,sharepoint,C#,.net,String,File,Sharepoint,我创建了一个应用程序,可以下载SP站点中的所有文档库,但有一次它给了我这个错误(我试图查看google,但找不到任何东西,现在如果有人知道解决此问题的任何技巧,请回复,否则谢谢查看) System.IO.PathTooLongException:指定的路径、文件名或两者都太长。完全限定文件名必须少于260个字符,目录名必须少于248个字符。 在System.IO.Path.NormalizePathFast(字符串路径,布尔fullCheck) 位于System.IO.Path.GetFullP

我创建了一个应用程序,可以下载SP站点中的所有文档库,但有一次它给了我这个错误(我试图查看google,但找不到任何东西,现在如果有人知道解决此问题的任何技巧,请回复,否则谢谢查看)

System.IO.PathTooLongException:指定的路径、文件名或两者都太长。完全限定文件名必须少于260个字符,目录名必须少于248个字符。 在System.IO.Path.NormalizePathFast(字符串路径,布尔fullCheck) 位于System.IO.Path.GetFullPathInternal(字符串路径) 在System.IO.FileStream.Init(字符串路径、文件模式、文件访问权限、Int32权限、布尔用户权限、文件共享、Int32缓冲大小、文件选项选项、安全属性secAttrs、字符串msgPath、布尔bFromProxy) 位于System.IO.FileStream..ctor(字符串路径、文件模式、文件访问访问、文件共享、Int32 bufferSize、文件选项) 在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
#地区下载方案
私有void btnDownload_单击(对象发送者,事件参数e)
{
TreeNode currentNode=tvWebs.SelectedNode;
SPObjectData objectData=(SPObjectData)currentNode.Tag;
尝试
{
CreateLoggingFile();
使用(SPWeb TopLevelWeb=objectData.Web)
{
如果(TopLevelWeb!=null)
dwnEachWeb(TopLevelWeb,TopLevelWeb.Title,tbDirectory.Text);
}
}
捕获(例外情况除外)
{
Trace.WriteLine(string.Format(“尝试将TopLevelWeb:{1},Title={2},对象数据传递给(dwnEachWeb_方法),异常:{0}”,例如ToString(),objectData.Web,objectData.Title时捕获的异常”);
}
最后
{
CloseLoggingFile();
}
}
私有void dwnEachWeb(SPWeb TopLevelWeb、string FolderName、string CurrentDirectory)
{
如果(TopLevelWeb!=null)
{
如果(TopLevelWeb.Webs!=null)
{
CurrentDirectory=CurrentDirectory+“\\”+TopLevelWeb.Title;
CreateFolder(当前目录);
foreach(TopLevelWeb.Webs中的SPWeb ChildWeb)
{
dwnEachWeb(ChildWeb,ChildWeb.Title,CurrentDirectory);
Dispose();
}
dwnEachList(TopLevelWeb,CurrentDirectory);
//dwnEachList(TopLevelWeb、FolderName、CurrentDirectory);
}
}
}
私有void dwnEachList(SPWeb oWeb,字符串CurrentDirectory)
{
foreach(oWeb.列表中的SPList oList)
{
if(oList为SPDocumentLibrary&!oList.Hidden)
{
dwnEachFile(oList.RootFolder,CurrentDirectory);
}
}
}
私有void dwnEachFile(文件夹的SPFolder,字符串CurrentDirectory)
{
如果(oFolder.Files.Count!=0)
{
CurrentDirectory=CurrentDirectory+“\\\”+文件夹名称;
CreateFolder(当前目录);
foreach(oFolder.Files中的spofile文件)
{
if(CreateDirectoryStructure(CurrentDirectory,ofile.Url))
{
var filepath=System.IO.Path.Combine(CurrentDirectory,ofile.Url);
字节[]binFile=ofile.OpenBinary();
System.IO.FileStream fstream=System.IO.File.Create(filepath);
写入(binFile,0,binFile.Length);
fstream.Close();
}
}
}
}
//正在创建将下载文件的目录
private bool CreateDirectoryStructure(字符串baseFolder、字符串文件路径)
{
如果(!Directory.Exists(baseFolder))返回false;
var path=filepath.Split('/');
对于(var i=0;i
由于错误的原因显而易见,以下是一些有助于解决问题的信息:

看到这个了吗

以下是链接中的引用:

最大路径长度限制在Windows API中(以下段落中讨论了一些例外情况),最大路径长度 对于路径,为MAX_path,定义为260个字符。本地人 路径的结构顺序如下:驱动器号、冒号、, 反斜杠、用反斜杠分隔的组件名称和终止符 空字符。例如,驱动器D上的最大路径是“D:\some” 256个字符的路径字符串“其中”“表示不可见的 正在终止当前系统代码页的空字符。(修订) 此处使用字符<>是为了视觉清晰,不能作为 有效的路径字符串。)

以及一些变通方法(摘自评论):

有很多方法可以解决各种问题。下面列出的解决方案的基本思想始终是相同的:减少路径长度以使
路径长度+名称长度
。你可以:

  • 共享子文件夹
  • 使用命令行通过SUBST分配驱动器号
  • 使用VB下的AddConnection为路径指定驱动器号
    • 有一个名为的库,它提供了一个.NET API来处理长路径


      这里有一篇很好的文章介绍了.NET和PowerShell的这个问题:“

      在Windows 8.1上,使用。NET3.5,我也有类似的问题<
      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
      
      mklink ShortPath\To\YourLinkedSolution C:\Path\To\Your\Solution /D
      
      -Right click on the root path you need to access
      -Choose Properties
      -Click on Share button and add your chosen users who can access it