简体   繁体   English

当出现错误时directory.getfiles停止搜索

[英]directory.getfiles stops searching when error

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). 使用此代码,我正在寻找具有相同名称和不同扩展名(bak和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? 或者其他的东西?

That path has 261 characters. 该路径有261个字符。 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). 普通Win32 API的最大值为260( MAX_PATH ),并且.NET中不支持Win32的长路径支持(您可以P / Invoke,但这意味着要对该文件/目录进行所有操作)。

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. 正如@Richard的注释一样,您可以直接使用Win32 API来解决此限制。 However, personnally I prefer to use the Delimon.Win32.IO C# library from TechNet. 但是,个人而言,我更喜欢使用TechNet的Delimon.Win32.IO C#库。 This library replaces basic file functions of System.IO and supports File & Folder names up to up to 32,767 Characters. 该库替代了System.IO的基本文件功能,并支持最多32,767个字符的文件和文件夹名称。 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(); 
        } 
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM