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

编程入门 行业动态 更新时间:2024-10-27 20:35:12
本文介绍了当出现错误时,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停止搜索

本文发布于:2023-10-16 00:44:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1495974.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:出现错误   directory   getfiles

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!