File.Exists功能在Windows8或更高版本中不起作用

时间:2017-05-29 11:01:23

标签: .net file windows-8 windows-10-desktop

  1. 文件存在函数总是返回false,即使文件存在。
  2. 我试图复制文件路径n粘贴在运行中并尝试手动打开,它甚至失败甚至文件存在物理上(Win7工作正常,Win8或更高版本)
  3. 从互联网上尝试了几种解决方案。
  4.     string FilePath = @"‪D:\Test\alllllllllthe Data.docx";
        if (File.Exists(FilePath))
        {
            string FileContent = File.ReadAllText(FilePath);

    }

1 个答案:

答案 0 :(得分:0)

是的,我会诚实地说,不理解导致这种情况的变化,因为你是对的:

string filePath = @"D:\Folder\somefile.txt";
File.Exists(filePath) == TRUE // this is not happening

我更进了一步并做了:

try
{
    var filePath = Path.GetFullPath("E:\\Folder\\somefile.txt");
    File.OpenRead(filePath);
}
catch (Exception ex)
{ }

抛出的异常是:
NotSupportedException: The given path's format is not supported.

这确实有效,所以请试一试:

var filePath = Path.GetFullPath("D:\\Folder\\somefile.txt");
File.Exists(filePath) == TRUE // this does work

或者,您也可以使用它:

var path = @"D:\Folder";
var fileName = "somefile.txt";
var filePath = Path.Combine(path, fileName);
File.Exists(filePath) == TRUE // this does work
相关问题