public void DisplayResource(IResource resource) { _displayedResource = resource; string path = null; try { path = Core.FileResourceManager.GetSourceFile(resource); } catch (Exception e) { Utils.DisplayException(e, "Error"); } if (path != null && path.Length > 0) { string fileType = FileSystemTypes.GetFileType(Path.GetExtension(path)); if (fileType == null || fileType.Length == 0) { fileType = Path.GetExtension(path); } _infoLabel.Text = Core.ProductName + " is unable to process files of type \"" + fileType + "\". To open this resource in associated application, click the button below."; _openButton.Visible = true; return; } _infoLabel.Text = Core.ProductName + " failed to determine type of this resource, it cannot be displayed :("; _openButton.Visible = false; }
public FileSystemProxy(FileSystemTypes fileSystemType, AzureBlobSettings azureBlobSettings = null) { if (fileSystemType == FileSystemTypes.SystemIO) { FileSystem = new FileSystem(); return; } if (azureBlobSettings == null) { throw new InvalidOperationException("Azure Blob Settings is required to initialize the Azure"); } FileSystem = new AzureBlobAdapter(azureBlobSettings); }
/* System.IO.DirectoryInfo.EnumerateFileSystemInfos is a great method for getting file and directory info. Unfortunately, that method can't be used to easily modify those files and directories, much less do multiple modifying operations on one traversal of the tree. That's what DirectoryWalker is for. It recurses down the tree doing a depth-first traversal until it reaches the "leaves" (i.e. directories that don't contain any subdirectories). Then, as the recursion unwinds, the provided action lambda is given a FileSystemInfo representing either the current file or directory that's being enumerated. The lambda can do anything it wants with the FileSystemInfo, including deleting the file or directory it represents. This is a safe operation since it happens on the way back "up" the tree, so deleting a directory won't have any affect on this method's algorithm. One neat trick this allows, and the reason I wrote this method in the first place, is that the lambda can delete files, and then delete any directories if they're empty. Both of those operations occur safely in one traversal of the directory hierarchy. (See the unit tests in Utilities.Core.Tests::FileIO.cs for an example of this and several other uses of DirectoryWalker). Since this method allows file operations, there's always the chance of an exception occurring. Should the method stop on the first exception, or store it for later perusal and continue? The answer I decided on is "both". The DirectoryWalkerErrorHandling parameter allows the caller to select what exception handling behavior DirectoryWalker should exhibit. The exceptions are not thrown, so this method doesn't need to be wrapped in a try/catch handler. Any exceptions that do occur are stored in the return value of List<Exception>. When called with a setting of DirectoryWalkerErrorHandling.Accumulate, DirectoryWalker will process all files and directories, storing all exception objects in the return value. */ public static List<Exception> DirectoryWalker(String rootDirectory, Action<FileSystemInfo> action, FileSystemTypes fileSystemTypes, DirectoryWalkerErrorHandling directoryWalkerErrorHandling) { var exceptions = new List<Exception>(); Action<String> rec = null; /* C# oddity: lambda definition must initially be set to null to allow for recursion. */ rec = (directory) => { if (directoryWalkerErrorHandling.HasFlag(DirectoryWalkerErrorHandling.StopOnFirst) && exceptions.Any()) return; try { var di = new DirectoryInfo(directory); foreach (var fsi in di.EnumerateFileSystemInfos()) { if (fsi is DirectoryInfo) rec(fsi.FullName); if (directoryWalkerErrorHandling.HasFlag(DirectoryWalkerErrorHandling.StopOnFirst) && exceptions.Any()) return; try { if ((fileSystemTypes.HasFlag(FileSystemTypes.Files) && (fsi is FileInfo)) || ((fileSystemTypes.HasFlag(FileSystemTypes.Directories) && (fsi is DirectoryInfo)))) action(fsi); } catch (Exception ex) { exceptions.Add(ex); } } } catch (Exception ex) { exceptions.Add(ex); } }; rec(rootDirectory); return exceptions; }
public static List<Exception> DirectoryWalker(String rootDirectory, Action<FileSystemInfo> action, FileSystemTypes fileSystemTypes) { return DirectoryWalker(rootDirectory, action, fileSystemTypes, DirectoryWalkerErrorHandling.Accumulate); }
/** * finds or creates resource for a file * for the UnknowFile type creates transient resources */ public IResource FindOrCreateFile(FileInfo fileInfo, bool createTransient) { string resourceType; bool indexIt = false; DateTime lastWriteTime = IOTools.GetLastWriteTime(fileInfo); string extension = IOTools.GetExtension(fileInfo); int size = (int)IOTools.GetLength(fileInfo); string name = IOTools.GetName(fileInfo); IResource file = FindFile(fileInfo); if (file != null) { if (!Core.ResourceStore.IsOwnerThread()) { return((IResource)_resourceAP.RunUniqueJob(_cUpdatingFileJob, _findOrCreateFileDelegate, fileInfo, createTransient)); } file.BeginUpdate(); if (file.Type == FileProxy._unknownFileResourceType && (resourceType = _ftm.GetResourceTypeByExtension(extension)) != null) { file.ChangeType(resourceType); indexIt = true; } if (name != file.GetPropText(Core.Props.Name)) { file.SetProp(Core.Props.Name, name); indexIt = true; } if (lastWriteTime != file.GetDateProp(Core.Props.Date)) { file.SetProp(Core.Props.Date, lastWriteTime); indexIt = true; } indexIt = indexIt || (!file.IsTransient && !file.HasProp("InTextIndex")); file.SetProp(FileProxy._propSize, size); string filetype = FileSystemTypes.GetFileType(extension); file.SetProp(FileProxy._propFileType, filetype ?? "Unknown"); } else { string directoryName = IOTools.GetDirectoryName(fileInfo); IResource folder = FindOrCreateDirectory(directoryName); if (folder == null) { return(null); } resourceType = _ftm.GetResourceTypeByExtension(extension); /** * look through pending file deletions */ IResourceList deletedFiles = Core.ResourceStore.FindResourcesWithProp(resourceType, FileProxy._propDeletedFile); deletedFiles = deletedFiles.Intersect( Core.ResourceStore.FindResources(null, FileProxy._propSize, size), true); deletedFiles = deletedFiles.Intersect( Core.ResourceStore.FindResources(null, Core.Props.Name, name), true); if (deletedFiles.Count > 0) { file = deletedFiles[0]; if (!file.IsTransient) { if (!Core.ResourceStore.IsOwnerThread()) { return((IResource)_resourceAP.RunUniqueJob( _cUpdatingFileJob, _findOrCreateFileDelegate, fileInfo, createTransient)); } file.BeginUpdate(); } } if (file == null) { if (resourceType != null && !createTransient) { if (!Core.ResourceStore.IsOwnerThread()) { return((IResource)_resourceAP.RunUniqueJob( _cUpdatingFileJob, _findOrCreateFileDelegate, fileInfo, createTransient)); } file = Core.ResourceStore.BeginNewResource(resourceType); indexIt = true; } else { if (!createTransient) { return(null); } if (resourceType == null) { resourceType = FileProxy._unknownFileResourceType; } file = Core.ResourceStore.NewResourceTransient(resourceType); } } file.SetProp(FileProxy._propParentFolder, folder); file.SetProp(FileProxy._propDirectory, directoryName); file.SetProp(Core.Props.Name, name); file.SetProp(Core.Props.Date, lastWriteTime); file.SetProp(FileProxy._propSize, size); string filetype = FileSystemTypes.GetFileType(extension); file.SetProp(FileProxy._propFileType, filetype ?? "Unknown"); } file.SetProp(FileProxy._propDeletedFile, false); if (!file.IsTransient) { file.EndUpdate(); if (indexIt) { Core.TextIndexManager.QueryIndexing(file.Id); } } return(file); }
/// <summary> /// Parses the specified args array into a PathLengthSearchOptions object instance. /// </summary> public static PathLengthSearchOptions ParseArgs(IEnumerable <string> args) { var searchOptions = new PathLengthSearchOptions(); foreach (var arg in args) { // Split the command-line arg on the equals sign. var parameter = arg.Split("=".ToCharArray(), 2); if (parameter.Count() < 2) { throw new ArgumentException("All parameters must be of the format 'Parameter=Value'"); } // Assign the Command and Value to temp variables for processing. var command = parameter[0]; var value = parameter[1]; // Fill in the Search Options based on the Command. switch (command) { default: throw new ArgumentException("Unrecognized command: " + command); case "RootDirectory": searchOptions.RootDirectory = value; break; case "RootDirectoryReplacement": searchOptions.RootDirectoryReplacement = string.Equals(value, "null", StringComparison.InvariantCultureIgnoreCase) ? null : value; break; case "SearchOption": searchOptions.SearchOption = string.Equals("TopDirectory", value, StringComparison.InvariantCultureIgnoreCase) ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories; break; case "TypesToInclude": FileSystemTypes typesToInclude = FileSystemTypes.All; if (string.Equals("OnlyFiles", value, StringComparison.InvariantCultureIgnoreCase)) { typesToInclude = FileSystemTypes.Files; } else if (string.Equals("OnlyDirectories", value, StringComparison.InvariantCultureIgnoreCase)) { typesToInclude = FileSystemTypes.Directories; } searchOptions.TypesToGet = typesToInclude; break; case "SearchPattern": searchOptions.SearchPattern = value; break; case "MinLength": int minLength = -1; if (int.TryParse(value, out minLength)) { searchOptions.MinimumPathLength = minLength; } break; case "MaxLength": int maxLength = -1; if (int.TryParse(value, out maxLength)) { searchOptions.MaximumPathLength = maxLength; } break; case "Output": OutputTypes outputType = OutputTypes.Paths; if (Enum.TryParse(value, out outputType)) { searchOptions.OutputType = outputType; } break; } } return(searchOptions); }
/* System.IO.DirectoryInfo.EnumerateFileSystemInfos is a great method for * getting file and directory info. Unfortunately, that * method can't be used to easily modify those files and directories, much less do multiple * modifying operations on one traversal of the tree. * * That's what DirectoryWalker is for. It recurses down the tree * doing a depth-first traversal until it reaches the "leaves" (i.e. directories * that don't contain any subdirectories). Then, as the recursion unwinds, * the provided action lambda is given a FileSystemInfo representing either the * current file or directory that's being enumerated. The lambda can do anything * it wants with the FileSystemInfo, including deleting the file or directory it represents. * This is a safe operation since it happens on the way back "up" the tree, so deleting * a directory won't have any affect on this method's algorithm. * * One neat trick this allows, and the reason I wrote this method in the first place, * is that the lambda can delete files, and then delete any directories if they're empty. * Both of those operations occur safely in one traversal of the directory hierarchy. * (See the unit tests in Utilities.Core.Tests::FileIO.cs for an example of this and * several other uses of DirectoryWalker). * * Since this method allows file operations, there's always the chance of an * exception occurring. Should the method stop on the first exception, or store it * for later perusal and continue? The answer I decided on is "both". * * The DirectoryWalkerErrorHandling parameter allows the caller to select what * exception handling behavior DirectoryWalker should exhibit. The exceptions are * not thrown, so this method doesn't need to be wrapped in a try/catch handler. * Any exceptions that do occur are stored in the return value of List<Exception>. * * When called with a setting of DirectoryWalkerErrorHandling.Accumulate, DirectoryWalker * will process all files and directories, storing all exception objects in the return value. */ public static List <Exception> DirectoryWalker(String rootDirectory, Action <FileSystemInfo> action, FileSystemTypes fileSystemTypes, DirectoryWalkerErrorHandling directoryWalkerErrorHandling) { var exceptions = new List <Exception>(); Action <String> rec = null; /* C# oddity: lambda definition must initially be set to null to allow for recursion. */ rec = (directory) => { if (directoryWalkerErrorHandling.HasFlag(DirectoryWalkerErrorHandling.StopOnFirst) && exceptions.Any()) { return; } try { var di = new DirectoryInfo(directory); foreach (var fsi in di.EnumerateFileSystemInfos()) { if (fsi is DirectoryInfo) { rec(fsi.FullName); } if (directoryWalkerErrorHandling.HasFlag(DirectoryWalkerErrorHandling.StopOnFirst) && exceptions.Any()) { return; } try { if ((fileSystemTypes.HasFlag(FileSystemTypes.Files) && (fsi is FileInfo)) || ((fileSystemTypes.HasFlag(FileSystemTypes.Directories) && (fsi is DirectoryInfo)))) { action(fsi); } } catch (Exception ex) { exceptions.Add(ex); } } } catch (Exception ex) { exceptions.Add(ex); } }; rec(rootDirectory); return(exceptions); }
public static List <Exception> DirectoryWalker(String rootDirectory, Action <FileSystemInfo> action, FileSystemTypes fileSystemTypes) { return(DirectoryWalker(rootDirectory, action, fileSystemTypes, DirectoryWalkerErrorHandling.Accumulate)); }