.Net Core 异常 System.IO.IOException:“文件名、目录名或卷标语法不正确。

一. Net Core 文件读取异常:

System.IO.IOException:“文件名、目录名或卷标语法不正确。 

 

 

问题原因:

.Net Core 支持跨平台,默认文件操作不是绝对路径操作,而是相对路径,相对于当前运行程序dll所在的目录。

 

解决方案:

使用PhysicalFileProvider 文件转换类

PhysicalFileProvider physical = new PhysicalFileProvider(@"G:\桌面");
string result=File.ReadAllText(physical.GetFileInfo("新建文本文档.txt").PhysicalPath);
Console.WriteLine(result);

 

 

更多:

.Net Core 5.0 Json序列化和反序列化 | System.Text.Json 的json序列化和反序列化

.Net Core HTML/JS/CSS 静态文件压缩方案,YUICompressor.NET

.Net Core System.IO.Compression.ZipFile实现Zip格式压缩和Zip格式解压缩

 

posted @ 2021-02-20 13:34  天马3798  阅读(3217)  评论(0编辑  收藏  举报