Technolog

Blogging over technologie.
Welcome to Technolog Sign in | Join | Help
in Search

eprogrammer

Howto: Check the case sensitive existence of a file

Sometimes, especially for files running on external FTP servers, where file names are case sensitive, a file named myFILE.html, is not the same file as myfile.html in the same path!

This function, can be used on an NTFS path for that purpose where File.Exists would fail, because it is case insensitive. (However, it does not enable you to have two files with just a different case in the same path)

/// <summary>
/// Checks existance of file using a case sensitive compare
/// </summary>
/// <param name="file">must be full filename</param>
/// <returns></returns>
static bool FileExists(string file)

{

string pathCheck = Path.GetDirectoryName(file);

string filePart = Path.GetFileName(file);

      if (string.IsNullOrEmpty(pathCheck))

      {

      throw new ArgumentException("The file must include a full path", file);

      }

      string[] checkFiles = Directory.GetFiles(pathCheck, filePart, SearchOption.TopDirectoryOnly);

      if (checkFiles != null && checkFiles.Length > 0)

      {

            //must be a binary compare

            return Path.GetFileName(checkFiles[0]) == filePart;

       }

       return false;

}

 

Published Monday, July 06, 2009 11:03 AM by eprogrammer

Comments

No Comments
Anonymous comments are disabled

About eprogrammer

Egbert Nierop is enthousiast C# en ASP.NET evangelist . Egbert Nierop is zelfstandige en houdt van projecten met een hoog technologische uitdaging.
Powered by Community Server, by Telligent Systems