Geekpedia Forums Logo

Find files from disk drives in c#

by tony j sebastian on Monday, November 24th - 1:09 AM



Hai all, How to find the file csc.exe from the disk drive , by not telling the drive or path in which it is actually stored ... i wish to get the complete file path of the file named csc.exe from the system....could any one help me to solve this problem.... if possible plz shoe the code snippet to me also thanks & regards Tony

This should work: // Goes trough all available drives and reports the first apperance // of the file with the name given in match, or null, if no file is // found string f_1(string match) { string a = null; foreach(DriveInfo di in DriveInfo.GetDrives()) { if(a = f_2(di.RootDirectory, match)) != null) return a; } return null; } // Recursively searches trough all child directories of a given // directory and searches for a file specified in match. // If it finds the file it returns the full path of the parent // directory string f_2(string di, string match) { if(File.Exists(di)) { FileSystemInfo[] files = new DirectoryInfo(di).GetFileSystemInfo(); foreach(FileSysteminfo fi in files) { if(fi is FileInfo && fi.Name.Equals(match)) return ((FileInfo)fi).DirectoryName; if(fi is DirectoryInfo) return f_2(fi.FullPath); } } return null; }

Ok, i didn't expect that the forum doesn't even recognise line breaks. The code at pastebin: http://pastebin.com/fd96d189