8

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

Deeptechtons
  • 10,945
  • 27
  • 96
  • 178
  • 1
    And what part of it is not obvious to you? You already provided the solution in your attempt to write a question. – Claus Jørgensen Apr 11 '11 at 09:03
  • 2
    Why do you need a FileInfo object? – Achinth Gurkhi Apr 11 '11 at 09:05
  • 1
    you can create a FileInfo object for each string in the resulting array... what is your problem ? – Steve B Apr 11 '11 at 09:06
  • 1
    @Claus How do you get the full path to the File from string of file names? Instantiating a new FilInfo for just getting the file path is too costly in my situation – Deeptechtons Apr 11 '11 at 09:07
  • 1
    @Steve the operation is costly in my situation – Deeptechtons Apr 11 '11 at 09:08
  • 1
    @Deeptechtons `Path + "\\" + Filename` - you have the path and the filename, ... – Kieren Johnstone Apr 11 '11 at 09:16
  • 3
    Its not more costly than the answer you accepted, it still creates FileInfo object internally.. – Richard Friend Apr 11 '11 at 09:18
  • 2
    Directory.GetFiles already gives you the full name. Quote from [MSDN](http://msdn.microsoft.com/en-us/library/wz42302f.aspx): "Returns the names of files (*including their paths*) that match the specified search pattern in the specified directory" (emphasis mine). What is the problem with it? – R. Martinho Fernandes Apr 11 '11 at 09:21
  • 1
    @Richard @Martinho yeah i agree with you guys. But when it comes to step number 1.1 Providing files as download links would be easy using the `fileInfo[]` as dataSource do you agree with that? – Deeptechtons Apr 11 '11 at 09:26
  • 1
    @Someone who downvoted the question could you please say why ? – Deeptechtons Apr 11 '11 at 09:30
  • 2
    I downvoted because 1) the question is unclear: I don't understand the problem that needs solving; and 2) I don't like the accepted answer. – R. Martinho Fernandes Apr 11 '11 at 09:37
  • 1
    @Martinho and thanks for explaining great comment. But certain things fit certain requirements. When i came to know that providing download links would we easy using FileInfo objects then i had to go the costlier way hence accepted that as answer. concerned to Richards answer it was my first instinct to mark it as answer but considering extensibility i did not but voted up as good solution. I will make it a point to explain clearer and in details next time thanks again :D – Deeptechtons Apr 11 '11 at 09:44
  • 1
    @Deeptechtons: if you edit the question and clear that up, I'll be glad to remove the downvote :) – R. Martinho Fernandes Apr 11 '11 at 09:45
  • 1
    @Martinho Updated if still doesn't sound clear i can edit to my hearts content – Deeptechtons Apr 11 '11 at 10:01

3 Answers3

20
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

Kent
  • 2,952
  • 2
  • 25
  • 42
5

To see if a file exists with that name, can you not just use..

However, Directory.GetFiles already includes the full path

string [] files = Directory.GetFiles(Path,"name*");
bool exists =  files.Length > 0;

if ( exists)
{
   //Get file info - assuming only one file here..
    FileInfo fi = new FileInfo(files[0]);

    //Or loop through all files
    foreach (string s in files)
    {
         FileInfo fi = new FileInfo(s);
         //Do something with fileinfo
    }
}
Richard Friend
  • 15,800
  • 1
  • 42
  • 60
  • 1
    thanks for pointing me in right direction, Your's could be answer but when extensibility comes @Hatake Kakashi answers looks good to me – Deeptechtons Apr 11 '11 at 09:14
2

You can use DirectoryInfo.GetFiles() to have a FileInfo[] instead of a String[].

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
Felice Pollano
  • 32,832
  • 9
  • 75
  • 115