0

I have this code witch works perfectly on my dev enviroment:

String[] FileNames = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".bak") || s.EndsWith(".dwg")).ToArray();
var queryDupNames = from f in FileNames
                    group f by Path.GetFileNameWithoutExtension(f) into g
                    where g.Count() > 1
                    select new { Name = g.Key, FileNames = g }
                 ;
var lista = queryDupNames.ToList();

With this code I´m looking for files with the same name and different extension (bak and dwg). When I tried this with my company mapped drive I got this error:

Excepción no controlada: System.IO.DirectoryNotFoundException: No se puede encontrar una parte de la ruta de acceso 'O:\auskalononden\sistema de gestion\mantenimiento\01.-Maquinas y utiles\COMPROBADORAS\utiles y maquinas sin presion ni agua original\Deapnelizadora de alimantacion CODIGO-- ESPAÑA-- FRANCIA\JAULA GIRONA ESPAÑA FRANCIA\Jaula de utensilios KIDJQd sC-22\4403.-repu'.
   en System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   en System.IO.Directory.InternalGetFileDirectoryNames(String path, String userPathOriginal, String searchPattern, Boolean includeFiles, Boolean includeDirs, SearchOption searchOption)
   en System.IO.Directory.GetFiles(String path, String searchPattern, SearchOption searchOption)
   en bakdwg.Program.Main(String[] args) en D:\dev\bakdwg\bakdwg\Program.cs:línea 19

Is there any whay to tell: if you have an error with this path, follow with next path? or something?

Kioko Kiaza
  • 1,378
  • 8
  • 28
  • 57
  • 3
    please translate the error message. – Daniel A. White Aug 10 '15 at 12:39
  • 2
    Your path is too long, the maximum allowed path length in Windows is 255 characters, see http://stackoverflow.com/questions/12204186/error-file-path-is-too-long – Alex Aug 10 '15 at 12:41
  • it says something like "Exception not controlled" it can not be found a part of the path 'O:\auska...' or something like that. I´m thinking that maybe it reaches to the maximum length of a path string¿? – Kioko Kiaza Aug 10 '15 at 12:44
  • @Jaco I was supousing that. Is there any solution? – Kioko Kiaza Aug 10 '15 at 12:46
  • Here is the real answer. GetFiles is recursively searching all subfolders. When a folder that you don't have the credentials to read will cause an exception and stop the code from running. The only solution is to write you own recursive search of the subfolders with an exception handler to allow the code to continue if you get an access violation. I written this code lots of times before. If you need help let me know. The other answers people are giving are wrong. – jdweng Aug 10 '15 at 12:58
  • yap, but I also need to make it work with long paths. I ask for a short answer to keep the program finding instead of errors. I´m looking now the way to resolve long path issue and make the program work properly – Kioko Kiaza Aug 10 '15 at 13:02

2 Answers2

2

That path has 261 characters. The normal Win32 APIs max out at 260 (MAX_PATH) and there is no support in .NET for Win32's long path support (you can P/Invoke but that means doing all the operations on that file/directory that way).

Richard
  • 106,783
  • 21
  • 203
  • 265
  • so there is no solution? So I need to wirte a Windows Form app ? – Kioko Kiaza Aug 10 '15 at 12:47
  • @KiokoKiaza See the linked question's answers (there are some options). A WinForms app will have the same restriction. The only direct solution is to use Win32 APIs directly with the path syntax for supporting long paths. – Richard Aug 10 '15 at 12:49
2

As per my comment, the error occurs because your file path is too long. As @Richard comments, you could use Win32 APIs directly to get around this limit. However, personnally I prefer to use the Delimon.Win32.IO C# library from TechNet. This library replaces basic file functions of System.IO and supports File & Folder names up to up to 32,767 Characters. See a basic code example below:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

// Specifically adding the Delimon.Win32.IO Library to use in the current Code Page 
using Delimon.Win32.IO; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            string[] files = Directory.GetFiles(@"c:\temp"); 
            foreach (string file in files) 
            { 
                Console.WriteLine(file); 
            } 
            Console.ReadLine(); 
        } 
    } 
}
Alex
  • 21,273
  • 10
  • 61
  • 73