Differences .Net Framework and .Net Core File System

In .Net Fx, can be determined by try / catch instantiated DirectoryInfo / FileInfo path input by the user is legitimate, but I have run the code Kaodao .Net Core, completely different operating results found

var DI = new new DirectoryInfo ( @ " @ # ¥% 35 | ¥% / ¥ ^ [<(country)>] ......%, / - \ = + & * ...... # @! " ); // .Net Framework under throw ArgumentException exception: the path has illegal characters. 
di.Create ();   // throws IOException under .Net Core: file name, directory name or volume label syntax is incorrect.

The above code will throw the first sentence in .Net Fx 4.6.1 System.ArgumentException exception because the path has illegal characters, DirectoryInfo instances of failure

In .Net Core 2.2 is not an error in the first sentence, second sentence System.IO.IOException will throw an exception, probably because .Net Core is cross-platform, do a unified path for different operating systems judgment is too much trouble, simply Analyzing not (can scribble), nor be determined Create method, and an exception return directly to the system IO

 

In order to prove the above point of view

View .Net Fx source (also can decompile), found inside the constructor DirectoryInfo a lot of paths to check, such as illegal characters and path length
https://referencesource.microsoft.com/#mscorlib/system/io/directoryinfo.cs , 30fa608717e5ce8e

构造方法内部调用顺序:DirectoryInfo(string path) -> Init(string path, bool checkHost) -> Directory.GetFullPathAndCheckPermissions(path, checkHost) -> Path.GetFullPathInternal(path) -> NormalizePath(path, fullCheck: true) -> Path.NormalizePath(string path, bool fullCheck, int maxPathLength) -> Path.NormalizePath(path, fullCheck, maxPathLength, expandShortPaths: true) -> Path.LegacyNormalizePath(path, fullCheck, maxPathLength, expandShortPaths)

 

See source .Net Core Next, it was found in addition to determining whether the path is empty, no other inspection

https://github.com/dotnet/corefx/blob/215f374988eba0829f6880026909851ed04a8638/src/System.IO.FileSystem/src/System/IO/DirectoryInfo.cs

而 Create 方法也只是一句,调用底层文件系统创建目录

public void Create() => FileSystem.CreateDirectory(FullPath);

 

总结

.Net Core和.Net Fx是兼容关系,而不是继承关系,即使同样的类库,运行结果也有差异

或许 .Net Core 将来的版本会加上路径检查的功能,比如给 Path 添加一个 IsPathNormalized(string path) 静态方法,给 FileInfo/DirectoryInfo 添加一个 IsValid() 实例方法

Guess you like

Origin www.cnblogs.com/felixnet/p/12038758.html