Check if File exists in directory ignoring the extension

后端 未结 3 1881
死守一世寂寞
死守一世寂寞 2021-01-12 05:59

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

相关标签:
3条回答
  • 2021-01-12 06:29
    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

    0 讨论(0)
  • 2021-01-12 06:48

    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
        }
    }
    
    0 讨论(0)
  • 2021-01-12 06:48

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

    0 讨论(0)
提交回复
热议问题