Checks if a given file exists
/*
* BLLib.IO.FileUtil
* Andy Tidball
*
* Project: Black Lightning General Library
* Copyright: GNU General Public License
*/
using System;
using System.IO;
namespace BLLib.IO {
/// <summary>
/// A static utility class for working with files. Often provides additional functionality for System.IO.File.
/// </summary>
public static class FileUtil {
/// <summary>
/// Checks if a given file exists.
/// </summary>
/// <param name="Filename">The filename to check for existance.</param>
/// <param name="CaseSensitive">Whether or not the search should be case-sensitive.</param>
/// <returns>True if the given file exists, false if it does not.</returns>
public static bool Exists(string Filename, bool CaseSensitive) {
// first, check if this file exists not counting cast
bool FileExists = File.Exists(Filename);
// if they don't care about case, just return what we already know
if (!CaseSensitive) {
return FileExists;
}
// if this file didn't exist, just return now
if (!FileExists) {
return false;
}
// to check the case, we're going to get a file list from the directory to compare against
FileInfo Info = new FileInfo(Filename);
FileInfo[] Files = Info.Directory.GetFiles(Filename, SearchOption.TopDirectoryOnly);
// since we specified the filename as a pattern, we should have gotten exactly that one file returned
System.Diagnostics.Debug.Assert(Files.Length == 1);
// check to make sure the one filename received matches the entered filename, case-sensitively
return (Filename == Files[0].Name);
}
}
}
Related examples in the same category