Created
April 24, 2015 20:27
-
-
Save cameronsjo/b70b2b27f0213ec206fe to your computer and use it in GitHub Desktop.
LinqPad FileIO Extensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class FileIO | |
| { | |
| private static string LastDirectoryKey = "extensions.fileio.lastdirectory"; | |
| public static IEnumerable<string> GetFilesRecursivelyFromDirectory(string directory) | |
| { | |
| return Directory.EnumerateFiles(directory).Concat( | |
| Directory.EnumerateDirectories(directory) | |
| .SelectMany(subdir => GetFilesRecursivelyFromDirectory(subdir))); | |
| } | |
| // This doesn't work, and I haven't taken the time to figure out why... | |
| public static IEnumerable<string> GetDirectoriesRecursively(string directory) | |
| { | |
| return Directory.EnumerateDirectories(directory).SelectMany (subDir => GetDirectoriesRecursively(subDir)); | |
| } | |
| public static string GetFile() | |
| { | |
| var initialDirectory = Util.GetPassword(LastDirectoryKey); | |
| if (string.IsNullOrWhiteSpace(initialDirectory)) | |
| initialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments); | |
| var ofd = new System.Windows.Forms.OpenFileDialog() | |
| { | |
| CheckFileExists = true, | |
| InitialDirectory = initialDirectory | |
| }; | |
| var result = ofd.ShowDialog(); | |
| if (result != System.Windows.Forms.DialogResult.OK) | |
| { | |
| throw new Exception("Open file was cancelled."); | |
| } | |
| var fileName = ofd.FileName; | |
| var directory = System.IO.Directory.GetParent(fileName); | |
| Util.SetPassword(LastDirectoryKey, directory.FullName); | |
| if (string.IsNullOrWhiteSpace(fileName) || !System.IO.File.Exists(fileName)) | |
| { | |
| throw new Exception("Unable to get file: " + fileName); | |
| } | |
| Trace.WriteLine("File found: " + fileName); | |
| return fileName; | |
| } | |
| public static IEnumerable<string> GetFiles(bool log = false) | |
| { | |
| var ofd = new System.Windows.Forms.OpenFileDialog() | |
| { | |
| CheckFileExists = true, | |
| Multiselect = true, | |
| InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments) | |
| }; | |
| var result = ofd.ShowDialog(); | |
| if (result != System.Windows.Forms.DialogResult.OK) | |
| { | |
| throw new Exception("Open file was cancelled."); | |
| } | |
| var fileNames = ofd.FileNames; | |
| for (int i = 0; i < fileNames.Count(); i++ ) | |
| { | |
| var fileName = fileNames[i]; | |
| if (string.IsNullOrWhiteSpace(fileName) || !System.IO.File.Exists(fileName)) | |
| { | |
| throw new Exception("Unable to get file: " + fileName); | |
| } | |
| Trace.WriteLine("File found: " + fileName); | |
| } | |
| return fileNames; | |
| } | |
| public static async Task ProcessLinesAsync(string fileName, Action<string> action) | |
| { | |
| using (var s = new StreamReader(fileName)) | |
| { | |
| string line; | |
| while((line = await s.ReadLineAsync()) != null) | |
| { | |
| action.Invoke(line); | |
| } | |
| } | |
| } | |
| public static async Task ProcessLinesAsync(string fileName, Func<string, Task> action) | |
| { | |
| using (var s = new StreamReader(fileName)) | |
| { | |
| string line; | |
| while((line = await s.ReadLineAsync()) != null) | |
| { | |
| await action.Invoke(line); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment