If you have the path and the file name independently, you can do the following:
string ruta = @"D:\cursos\ASP.NET";
string archivo = "mi_curso.pdf";
if(File.Exists(ruta+"\"+archivo)){
Console.WriteLine("Existe el archivo: "+archivo);
} else {
Console.WriteLine("No existe");
}
Remember to include at the beginning of your file:
using System.IO;
If what you want to know is if a given path as a string contains the name of a specific file you can do the following:
string ruta = @"D:\cursos\ASP.NET\mi_curso.pdf";
string archivo = "mi_curso.pdf";
if(Path.FileName(ruta) == archivo){
Console.WriteLine("Existe el archivo: "+archivo);
} else {
Console.WriteLine("No existe");
}
You could do the latter with the IndexOf function that the strings have but you should check that the position found is the last possible one, that is:
string ruta = @"D:\cursos\ASP.NET\mi_curso.pdfs";
string archivo = "mi_curso.pdf";
if (ruta.ToLower().IndexOf(archivo.ToLower()) == ruta.Length - archivo.Length)
{
Console.WriteLine("Existe el archivo: " + archivo);
}
else
{
Console.WriteLine("No existe");
}
If you want to search exactly what the file is named, with case sensitivity; you can remove the .ToLower () wherever it appears in the last example, that is, it would look like this:
if (ruta.IndexOf(archivo) == ruta.Length - archivo.Length)