I'm trying to use my FileWorks class below to tell me if a directory has a certain file extension.
I'm trying to be as generic as possible and reuse the function...
GetFilesDoSomething
I'm not sure how to approach this one. This is the code I have below.
Does not look like it would work if I were to call it...
FileWorks filework = new FileWorks();
filework.GetFilesDoSomething(directory, CheckDirectoryForFilesWithExtension);
How can I pass the bool result back accross soo many functions?
public class FileWorks { bool CheckDirectoryForFilesWithExtension(FileInfo fi) { if (fi.Extension == "tif") { return true; } } public void GetFilesDoSomething(string DirName, Action<FileInfo> method) { DirectoryInfo d = new DirectoryInfo(DirName); FileInfo[] f = d.GetFiles(); foreach (FileInfo fi in f) { method.Invoke(fi); } } }
This codes isn't really "proper", but it does demonstrate using a delegate to make your GetFilesDoSomething() re-usable.
class Program{ static void Main(string[] args) { FileWorks.GetFilesDoSomething("C:\\", new FileWorks.SomethingDelegate(FileWorks.CheckDirectoryForFilesWithExtension)); Console.ReadKey(); }}public static class FileWorks{ public delegate void SomethingDelegate(object inParam, out object returnVal); public static void CheckDirectoryForFilesWithExtension(object fi, out object returnValue) { if (((FileInfo)fi).Extension.ToLower() == ".tif") returnValue = true; else returnValue = false; return; } public static void GetFilesDoSomething(string dirName, SomethingDelegate method) { DirectoryInfo d = new DirectoryInfo(dirName); FileInfo[] f = d.GetFiles(); foreach (FileInfo fi in f) { object returnValue; method(fi, out returnValue); Console.WriteLine("{0} is a TIF: {1}", fi.Name, (bool)returnValue); } }}
An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.
One of a set of tools we're offering as a way of saying thank you for being a part of the community.
Check this out:
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_24247143.html