![]() ![]() Even through the FileInfo object has already been created, the exception can occur because a FileInfo object will try to refresh its Length property by using the most current size in bytes the first time the property is accessed. The query calls out to a separate method to obtain the file size in bytes in order to consume the possible exception that will be raised in the case where a file was deleted on another thread in the time period since the FileInfo object was created in the call to GetFiles. Specify a descending sort order to put the smallest elements at the start of the list. Use Take to return the first n number of elements. Use First to return the first element in a list. Then it can return the single one or the sequence with the greatest lengths. To return one or more complete FileInfo objects, the query first must examine each one in the data source, and then sort them by the value of their Length property. ' In this particular case, it is safe to ignore the exception.įunction GetFileLength(ByVal fi As System.IO.FileInfo) As Long ![]() ' that can be raised when accessing the FileInfo.Length property. ' This method is used to catch the possible exception ' Keep the console window open in debug modeĬonsole.WriteLine("Press any key to exit.") Module QueryBySizeĭim root As String = "C:\Program Files\Microsoft Visual Studio 9.0"ĭim dir As New System.IO.DirectoryInfo(root)ĭim fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories)ĭim maxSize = Aggregate aFile In fileList Into Max(GetFileLength(aFile))Ĭonsole.WriteLine("The length of the largest file under ", item.Name, item.Length) You can easily modify these examples to base the query on some other property of the FileInfo object. The following example contains five separate queries that show how to query and group files, depending on their file size in bytes. How to order files into groups based on their file size in bytes, ignoring files that are less than a specified size. How to retrieve a sequence such as the 10 largest files. How to retrieve the FileInfo object largest or smallest file from one or more folders under a specified root folder. How to retrieve the size in bytes of the smallest file. How to retrieve the size in bytes of the largest file. This example shows five queries related to file size in bytes: ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |