Creé una aplicación que descarga todas las bibliotecas de documentos en un sitio SP, pero en un momento me dio este error (traté de buscar en google pero no pude encontrar nada, ahora si alguien sabe algún truco para resolver este problema por favor responder lo contrario gracias por mirarlo)La mejor manera de resolver la ruta del archivo demasiado tiempo excepción
System.IO.PathTooLongException: La ruta especificada, el nombre de archivo o ambos son demasiado largos. El nombre de archivo completo debe tener menos de 260 caracteres y el nombre del directorio debe tener menos de 248 caracteres. en System.IO.Path.NormalizePathFast (String path, Boolean fullCheck) en System.IO.Path.GetFullPathInternal (ruta de cadena) en System.IO.FileStream.Init (ruta de cadena, modo FileMode, acceso a FileAccess, derechos de Int32 , Boolean useRights, FileShare share, Int32 bufferSize, FileOptions opciones, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromStream) en System.IO.FileStream..ctor (ruta de cadena, modo FileMode, acceso de FileAccess, FileShare share, Int32 bufferSize, opciones de FileOptions) en System.IO.File.Create (string path)
se alcanza el límite de la secuencia, Código es la siguiente,
#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
convertir el UNC (o lo que sea) Ruta en un formato 8.3. [Convertir a formato 8.3 usando CMD] [1] [1]: http://stackoverflow.com/questions/10227144/convert-long-filename-to-short-filename-8 -3-using-cmd-exe – AutomationNation
Posible duplicado de [¿Cómo evitar System.IO.PathTooLongException?] (Http://stackoverflow.com/questions/530109/how-to-avoid-system-io-pathtoolongexception) – Deantwo