当出现错误时,directory.getfiles停止搜索 [英] directory.getfiles stops searching when error

查看:99
本文介绍了当出现错误时,directory.getfiles停止搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码可以在我的开发环境中完美地工作:

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();

使用此代码,我正在寻找具有相同名称和不同扩展名(bak和dwg)的文件.当我使用公司映射的驱动器尝试此操作时,出现此错误:

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?

推荐答案

根据我的评论,由于文件路径太长而发生错误.正如@Richard的注释一样,您可以直接使用Win32 API来解决此限制.但是,我个人更喜欢使用 Delimon.Win32.IO TechNet中的C#库.该库取代了System.IO的基本文件功能,并支持File&文件夹名称最多32,767个字符.请参见下面的基本代码示例:

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(); 
        } 
    } 
}

这篇关于当出现错误时,directory.getfiles停止搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆