0
  1. The file exists function always return false even file exists.
  2. I tried to copy file path n pasted in run and tried to open manually, it failed even File exists physically (Win7 works fine, Win8 or higher issue)
  3. Tried several solutions from the internet.

    string FilePath = @"‪D:\Test\alllllllllthe Data.docx";
    if (File.Exists(FilePath))
    {
        string FileContent = File.ReadAllText(FilePath);

    }
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Sampat Patil
  • 31
  • 1
  • 1
  • 4
  • Are you sure the path is correct, I am running Windows 10 1703 and just tested this. Works just fine. – Jason H May 29 '17 at 11:05
  • @JasonH - follow these steps to reproduce it. 1. Create folder in D/E drive eg: D: 2. Create folder under it eg: D:\Test 3. Create/Place file eg: D:\Test\myFile.docx 4. Right click file >> Property >> Security tab >> Object Name >> Copy whole file path 5. Press Win+R paste that path >> Enter – Sampat Patil May 29 '17 at 11:12
  • I tried serval time, 8/10 times its easily reproducible. – Sampat Patil May 29 '17 at 11:16

1 Answers1

0

Right I will be honest as to not understanding what changed that is causing this because you are correct:

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

I took this a step further and did:

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

The exception that is thrown is:
NotSupportedException: The given path's format is not supported.

This does work so give this a go:

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

Alternatively, you could use this as well:

var path = @"D:\Folder";
var fileName = "somefile.txt";
var filePath = Path.Combine(path, fileName);
File.Exists(filePath) == TRUE // this does work
Jason H
  • 4,996
  • 6
  • 27
  • 49
  • Basically, file operation are giving me same error **The given path's format is not supported** – Sampat Patil May 29 '17 at 11:41
  • Added an Alternative option as well – Jason H May 29 '17 at 11:44
  • 1
    I worked around the issue, there is extra character ? in the path start which is invisible. even wen i paste in Win+r or notepad. extra character is hidden so I came up with the solution. private string TrimInvalidChars(string FileFullPath) { int index = FileFullPath.IndexOf(":"); if (index > 0) { FileFullPath = FileFullPath.Substring(index - 1); } return FileFullPath; } – Sampat Patil May 29 '17 at 11:45
  • Does the problem only occur when you copy and paste the file path into the source code? – Harry Johnston May 30 '17 at 00:47