C#用不同版本Visual Studio的GetFiles()获取的结果不同

#问题:使用Visual Studio 2013时,调用System.IO,写到某个方法时,需要获取文件路径名,其中有段代码如下:

            string oldPath = @"D:\解密前";
            string newPath = @"D:\解密后";

            DirectoryInfo dir = new DirectoryInfo(oldPath);
            FileInfo[] fil = dir.GetFiles();
            string OldPath = oldPath + @"\" + fil[0];
            string NewPath = newPath + @"\" + fil[0];

            Console.WriteLine(oldPath);
            Console.WriteLine(newPath);
            Console.WriteLine(OldPath);
            Console.WriteLine(NewPath);
            Console.ReadKey();

输出效果如下:

D:\解密前
D:\解密后
D:\解密前\PNP-IO表-202109091002-V3.xlsx
D:\解密后\PNP-IO表-202109091002-V3.xlsx

img

而当我用Visual Studio 2019 写到同样一段代码时,却出现了不同的效果
2019输出结果如下:

D:\解密前
D:\解密后
D:\解密前\D:\解密前\PNP-IO表-202109091002-V3.xlsx
D:\解密后\D:\解密前\PNP-IO表-202109091002-V3.xlsx

2019的路径获取比2013的全?

和vs版本没关系吧,vs2013和vs2019附带的framework版本不一样,题主编译程序使用的版本是什么?还是用的.net core?

我这里vs2019测试framework fil[0]调用默认ToString后得到的只是文件名
.net core的话是文件全路径,题主用的应该是.net core吧,创建的虽然都是控制台,但是使用的编译框架不一样

建议是直接调用对应的属性,而不是直接调用FileInfo对象的默认ToString方法,不同框架可能返回值不一样。下面的就统一了


            string OldPath = oldPath + @"\" + fil[0].Name;
            string NewPath = newPath + @"\" + fil[0].Name;