惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

博客园 - 我有我奥妙

【BenchmarkDotNet】测试多方式的对象映射 【自动注入】.NET8/.NETCore 依赖注入:自动注入项目中所有接口和自定义类 【排名】处理同分数的排名 【Quartz】.Net8使用定时任务 【模型验证】未被异常捕获到 【Ant Design Vue】相关 【根节点】C#找树形数据的根节点Id 【C#】枚举值 【ECharts】图表自定义显示标题 【消息队列】介绍 【Nginx】Windows部署Vue 设计模式(一)-介绍 【.NetCore】创建本机的静态文件服务器 NLog(一)-使用示例 【nssm】windows上netcore注册为服务 【字符串排序】C#和前端js排序问题 【RestSharp】常用的几个请求方式 【笔记软件】Obsidian的使用 【浏览器扩展】编写Firefox和Chrome的扩展程序
【长路经】C#读取文件抛出FileNotFoundException异常
我有我奥妙 · 2025-01-01 · via 博客园 - 我有我奥妙

前言

在winform中读取文件信息时,突然抛出了FileNotFoundException的异常,但是本地是有这个文件的。
随后找到了这个文件,查看属性,[位置]属性,多了"\?"的前缀,百度得知这是windows对长路经的处理。

需要注意:
目前在NetFx框架下,才有这个问题。
在NetCore框架下,是正常运行。

复现问题的代码

代码如下

static void Test02()
{
	List<string> dirList = new List<string>()
	{
		"F:\\100-cnblogs_blog",
		"01-all-备份备份备份备份备份备份备份",
		"02-html-备份备份备份备份备份备份备份",
		"03-bak-备份备份备份备份备份备份备份",
		"04-file-备份备份备份备份备份备份备份",
	};
	string basePath = Path.Combine(dirList.ToArray());
	if (Directory.Exists(basePath) == false)
		Directory.CreateDirectory(basePath);
	//假设有这2个文件
	//这个文件是正常=>1.txt
	//这个文件会报错=>6bRLpUwRTXshio75MZtzxmqjtfRlIMXDKFPdAG1f63gdXvxoY5pPPUaermZuHsUfrLI90xSYW8qiYzucUV4GceuHqvpFVaojMkFS5g9mmE5QL5K2YEOkLWFuF2Oboi1JsbCEhMoD77SGczO7GgZX60XPQZuo7hZFP3LKqJ4EHYKL8yjdVAYAwpm737JikdH3OUQ9zOhh.txt
	var files = Directory.GetFiles(basePath, "*.*", SearchOption.AllDirectories);
	foreach (var item in files)
	{
		string fullName = item;
		FileInfo fileInfo = new FileInfo(fullName);
		Console.WriteLine(fileInfo.Name);
		string pre = "    =>";
		bool retry = false;
		try
		{
			Console.WriteLine(pre + "Length:" + fileInfo.Length);
			Console.WriteLine(pre + "未报错,正常获取");
		}
		catch (FileNotFoundException)
		{
			fullName = @"\\?\" + fullName;
			retry = true;
			Console.WriteLine(pre + "文件不存在,添加长路经前缀后重试");
		}
		if (retry)
		{
			fileInfo = new FileInfo(fullName);
			try
			{
				Console.WriteLine(pre + "Length:" + fileInfo.Length);
				Console.WriteLine(pre + "重试后,正常获取");
			}
			catch (Exception ex)
			{
				Console.WriteLine(pre + "重试仍然报错");
				Console.WriteLine(ex.Message);
			}
		}
	}
}

运行结果

解决办法

先是考虑了捕获异常再重试的思路,但是在可预知的情况不应该使用trycatch方式处理问题。
所以采用了判断文件是否存在的方式

代码如下

static void Test03()
{
	List<string> dirList = new List<string>()
	{
		"F:\\100-cnblogs_blog",
		"01-all-备份备份备份备份备份备份备份",
		"02-html-备份备份备份备份备份备份备份",
		"03-bak-备份备份备份备份备份备份备份",
		"04-file-备份备份备份备份备份备份备份",
	};
	string basePath = Path.Combine(dirList.ToArray());
	if (Directory.Exists(basePath) == false)
		Directory.CreateDirectory(basePath);
	var files = Directory.GetFiles(basePath, "*.*", SearchOption.AllDirectories);
	foreach (var item in files)
	{
		string fullName = item;
		if (File.Exists(fullName) == false)
			fullName = @"\\?\" + fullName;
		FileInfo fileInfo = new FileInfo(fullName);
		Console.WriteLine(fileInfo.Name);
		string pre = "    =>";
		Console.WriteLine(pre + "Length:" + fileInfo.Length);
	}
}

运行结果