Best way to allow file path exception is too long exception

I created an application that downloads all document libraries on the SP site, but once informed me of this error (I tried to look at google but could not find anything, now if anyone knows any trick to solve this problem, please answer otherwise thanks for watching)

System.IO.PathTooLongException: The specified path, file name, or both are too long. A fully qualified file name must be less than 260 characters, and a directory name must be less than 248 characters. in System.IO.Path.NormalizePathFast (String path, Boolean fullCheck) in System.IO.Path.GetFullPathInternal (String path) in System.IO.FileStream.Init (String path, FileMode mode, access to FileAccess, Int32 rights, logical useRights, FileShare share, Int32 bufferSize, FileOptions, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy options) in System.IO.FileStream..ctor (String path, FileMode mode, access to FileAccess, FileShare share, Int32 bufferSize, FileOptions ) in System.IO.File.Create (String path)

it reaches the limit for the string, the code is listed below,

#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 
+87
string c # file sharepoint
Jan 05 2018-12-15T00:
source share
11 answers

As the cause of the error is obvious, here is some information to help you solve the problem:

See MS article on file names, paths, and namespace

Here is a quote from the link:

Limiting the maximum path length In the Windows API (with some exceptions described in the following paragraphs), the maximum path length is MAX_PATH, which is defined as 260 characters. The local path is structured in the following order: drive letter, colon, backslash, name components separated by backslashes, and the termination is a null character. For example, the maximum path on drive D is "D: \ some 256-character line string <NUL>" where "<NUL>" represents an invisible terminating null character for the current system code page. (The characters <> are used for visual clarity and cannot be part of a valid path string.)

And a few workarounds (taken from comments):

There are ways to solve various problems. The basic idea of โ€‹โ€‹the solutions listed below is always the same: reduce the path length to have path-length + name-length < MAX_PATH . You can:

  • Share Subfolder
  • Use the command line to assign a drive letter using SUBST
  • Use AddConnection under VB to map drive letter
+51
Jan 05 2018-12-15T00:
source share

There is a library called Zeta Long Paths that provides a .NET API for working with long paths.

Here is a good article on this issue for both .NET and PowerShell: " . NET, PowerShell Path exception too long and .NET PowerShell Robocopy Clone "

+23
Jan 13 '13 at 19:01
source share

The solution that worked for me was to edit the registry key to enable long-path behavior by setting the value to 1. This is a new selection feature for Windows 10

HKLM\SYSTEM\CurrentControlSet\Control\FileSystem LongPathsEnabled (Type: REG_DWORD)

I got this solution from the named section of the article posted by @ james-hill.

https://docs.microsoft.com/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation

+12
Oct 12 '18 at 11:41
source share

On Windows 8.1, using .NET 3.5, I had a similar problem.
Although my file name was only 239 characters, when I went to create an instance of the FileInfo object with only the file name (no path), an exception of type System occurred. 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 , Int64 targetFolderID, String SID) in C: \ Users \ alle \ Documents \ Visual Studio 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 : riga 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 

I solved the problem of trimming the file name to 204 characters (extension included).

+2
Jan 22 '14 at 10:39
source share

You can create a symbolic link with a shorter directory. First open a command prompt , for example, Shift + RightClick in your desired folder with a shorter path (you may need to run it as an administrator).

Then enter relative or absolute paths:

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

And then run the solution from a shorter path. The advantage here: you do not need to move anything.

+2
Jun 17 '17 at 10:11
source share

If you have a problem with bin files due to the long path, in Visual Studio 2015 you can go to the project properties page, which crashed, and change the relative output directory to a shorter one.

For example, bin \ debug \ becomes C: \ _ bins \ MyProject \

+1
Mar 12 '18 at 19:44
source share

It worked for me that my project was transferred to the desktop (C: \ Users \ lachezar.l \ Desktop \ MyFolder) in (C: \ 0 \ MyFolder), which, as you can see, uses a shorter path. problem.

+1
Jun 22 '18 at 14:23
source share

Until the update is mentioned, there is a very well-developed library for handling too long paths. AlphaFS is a .NET library that provides more comprehensive Win32 file system functionality for the .NET platform than the standard System.IO classes. The most noticeable drawback of the standard .NET System.IO is the lack of support for extended NTFS functions, in particular, support for extended paths (for example, file / directory paths longer than 260 characters).

0
Feb 25 '19 at 16:07
source share

The best answer I can find is in one of the comments here. Add it to the answer so that someone does not miss a comment and be sure to try it. This fixed the problem for me.

We need to map the solution folder to the disk using the "subst" command in the prompt- command, for example, subst z:

Then open the solution from this disk (in this case z). This would shorten the path as much as possible and could solve the problem of a long file name.

0
Apr 30 '19 at 5:47
source share

Based on my experience, I will not recommend my answer below for any public web applications.

If you need it for your own tools or for testing, I would recommend sharing it on my computer.

 -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 

After that, a shared directory will be created, such as \ {PCName} {YourSharedRootDirectory} This, of course, can be much smaller than your full path, I hope for myself I could reduce it to 30 characters from 290 characters. :)

0
Jul 15 '19 at 16:48
source share

You can reduce the name of the project to:

 Solution Properties -> Application -> Assembly Name 
-5
Aug 16 '17 at 15:50
source share



All Articles