Error 'does not exist' when searching for specified path file

0

I have the following code to check if a file exists in a specified path but it always returns to me as it does not exist. I have looked at the file permissions and it is all correct.

if ( System.IO.File.Exists("C:\Temp\test.txt")) {
  //Hago una cosa
}
else
{
  //Hago otra
}
    
asked by Popularfan 20.08.2018 в 12:13
source

1 answer

1

This is probably a problem of permissions ( Documentation ).

You can try to open the file in the code, for example with the File.OpenRead instruction and see if any exception jumps (by placing a try/catch ).

That exception is going to give you more information about the problem that the file has and why it can not be opened.

Example of the code to test it:

try
{
    FileStream f = File.OpenRead("C:\Temp\test.txt");
}
catch (Exception e)
{
    MessageBox.Show(e.Message);
}

If it is not a matter of permissions and the exception continues to give you the error that the file is not found, you should check that the file is really called test.txt , one of the main causes is the option of Ocultar las extensiones of the browser , that having it activated can cause confusion and that the file is really called test.txt.txt and not test.txt .

    
answered by 20.08.2018 / 12:35
source