Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if File exists in directory ignoring the extension

I am using .NET 2.0 and Linq is out of question. I would like to check if file exists inside a directory without knowledge of the file extension.

I just need this logic done.

1.Check File exists in Directory using String Filename provided using search pattern leaving out the extension of the File

2.Get the Files if they exists and Databind to provide Download links.If file does not exist then start uploading the File.

Update: Directory.GetFiles() and DirectoryInfo.GetFiles() indeed solves the part where in i check for File existence. As for the performance regarding FileInfo objects, these were only solution to my requirements of databinding to provide download links

like image 739
Deeptechtons Avatar asked Apr 11 '11 09:04

Deeptechtons


1 Answers

DirectoryInfo root = new DirectoryInfo("your_directory_path");
FileInfo[] listfiles = root.GetFiles("dummy.*");
if (listfiles.Length > 0)
{
  //File exists
  foreach (FileInfo file in listfiles)
        {
            //Get Filename and link download here
        }
}
else
{
  //File does not exist
  //Upload
}

Hope this works

like image 122
Kent Avatar answered Sep 19 '22 13:09

Kent