-
Feb 20th, 2017, 01:35 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Extracting extended file properties for Listview Items - slow code
I have this function to get extended file properties (I need only 2 properties) and add It to my ListviewItems. Code works, but It's slow when I'm loading a directory with over 50+ items. Any way to speed things up a bit ??
My code:
Code:
Public Shared Function GetProperties(Fpath As String, prop As Integer)
Dim arrHeaders As New List(Of String)()
Dim shell As New Shell
Dim rFolder As Folder = shell.[NameSpace](Path.GetDirectoryName(Fpath))
Dim rFiles As FolderItem = rFolder.ParseName(Path.GetFileName(Fpath))
For i As Integer = 0 To 10
Dim value As String = rFolder.GetDetailsOf(rFiles, i).Trim()
arrHeaders.Add(value)
Next
Dim Desired_Prop As String
Desired_Prop = arrHeaders.Item(prop)
Return Desired_Prop
End Function
And how I add items:
Code:
For Each files As IO.FileInfo In Directory_Items.EnumerateFiles
Dim LVI As New ListViewItem
'adding tag, icons, name etc. - everything else works nice
'& fast except extended file properties
...
...
Listview1.Items.Add(LVI)
LVI.SubItems.Add(GetProperties(files.FullName, 2))
LVI.SubItems.Add(GetProperties(files.FullName, 1))
Next
Last edited by LuckyLuke82; Feb 20th, 2017 at 07:41 AM.
-
Feb 20th, 2017, 07:16 AM
#2
Re: Extracting extended file properties for Listview Items - slow code
As you currently have it, GetProperties is doing far more work than needed... everything from the loop onwards could be replaced by this:
Code:
Dim value As String = rFolder.GetDetailsOf(rFiles, prop).Trim()
Return value
However it may be more efficient overall to not do this, as the slow part is likely to be the code before the loop... if that is the case, it would be better to return the entire list (arrHeaders) and store it to a variable, then read both values from it.
What extended file properties are you reading?
-
Feb 20th, 2017, 07:34 AM
#3
Thread Starter
Fanatic Member
Re: Extracting extended file properties for Listview Items - slow code
Thanks, I will try this.
What extended file properties are you reading?
I'm reading file size (kind of better then usual .Length calculation) and file type or whatever called properly (e.g. "New Microsoft Word Document"). Everything else I do manually (Name & Date modified). My real code is basically From 0 to 3 in loop, but It's still same result.
-
Feb 20th, 2017, 08:07 AM
#4
Thread Starter
Fanatic Member
Re: Extracting extended file properties for Listview Items - slow code
Tried, first suggestion doesn't improve aynthing. With other suggestion you told - how should I store and retrieve from entire list ? I'm reading properties for folders too, so that would probably require arrheaders to clear each time before loading again, which wouldn't be faster ?
-
Feb 20th, 2017, 08:20 AM
#5
Re: Extracting extended file properties for Listview Items - slow code
Assuming the file size is the thing that shows "1.2 GB" etc, it is best to ask Windows (via the method you are using, or another), because it apparently gives values that aren't entirely predictable (and also presumably deals with formatting for language/region/etc).
The file type could arguably be found in a more efficient way (especially if you often show multiple instances of the same file type at the same time), but the effect would probably be negligible.
However, having thought about it a bit more I can see that it would be better to only obtain the two properties you want, which you could do like this:
Code:
Public Shared Sub GetFileSizeAndTypeName(Fpath As String, ByRef FileSize As String, ByRef TypeName as String)
Dim shell As New Shell
Dim rFolder As Folder = shell.[NameSpace](Path.GetDirectoryName(Fpath))
Dim rFiles As FolderItem = rFolder.ParseName(Path.GetFileName(Fpath))
FileSize = rFolder.GetDetailsOf(rFiles, 2).Trim()
TypeName = rFolder.GetDetailsOf(rFiles, 1).Trim()
End Function
...and call it like this:
Code:
Listview1.Items.Add(LVI)
Dim FileSize As String, TypeName as String
GetFileSizeAndTypeName(files.FullName, FileSize, TypeName)
LVI.SubItems.Add(FileSize)
LVI.SubItems.Add(TypeName)
If you are showing multiple files from the same folder, it would probably be faster if you set rFolder once, and pass it in to the sub/function.
-
Feb 20th, 2017, 08:43 AM
#6
Thread Starter
Fanatic Member
Re: Extracting extended file properties for Listview Items - slow code
Yes, I'm showing multiple files from same folder, that is the whole point why code is slow. This suggestion improves a bit, but still slow. How could I pass public rFolder, It needs path ?
-
Feb 20th, 2017, 09:35 AM
#7
Re: Extracting extended file properties for Listview Items - slow code
I would assume that your code knows the directory path before it starts finding the files to display, so you could set rFolder at that point.
-
Feb 21st, 2017, 08:04 AM
#8
Thread Starter
Fanatic Member
Re: Extracting extended file properties for Listview Items - slow code
Tried that one too, still slow. I guess that adding extended property just slows down everything when you have a lot of Listview items. The fastest was I could accomplish was this:
Code:
Public shell As New Shell
'Lopp through files in directory
...
...
...
Dim folders As Folder = shell.[NameSpace](Path.GetDirectoryName(MyFiles.FullName))
Dim files As FolderItem = folders.ParseName(Path.GetFileName(MyFiles.FullName))
Listview1.Items.Add(LVI)
LVI.SubItems.Add(folder.GetDetailsOf(files, 2).Trim())
LVI.SubItems.Add(folder.GetDetailsOf(files, 1).Trim())
Do you know maybe some other solutions ?
Last edited by LuckyLuke82; Feb 21st, 2017 at 08:48 AM.
-
Feb 21st, 2017, 08:55 AM
#9
Re: Extracting extended file properties for Listview Items - slow code
The line Dim folders ... would be better before the loop, but I can't make a more accurate suggestion than that with the code you have shown.
-
Feb 21st, 2017, 10:31 AM
#10
Thread Starter
Fanatic Member
Re: Extracting extended file properties for Listview Items - slow code
That won't work, Dim needs to be there otherwise how can I get file path ? My loop is this:
Code:
For each MyFiles As FileInfo In MyDirectory.EnumerateFiles
Next
So folders needs a path to MyFiles to get It's properties, It needs to be inside loop - I'm adding properties to each item.
-
Feb 21st, 2017, 10:47 AM
#11
Re: Extracting extended file properties for Listview Items - slow code
I'm still guessing at what your code is, but I'm fairly certain that setting up MyDirectory also needs the path to the folder - which is almost certainly the same thing that folders needs.
-
Feb 21st, 2017, 02:57 PM
#12
Thread Starter
Fanatic Member
Re: Extracting extended file properties for Listview Items - slow code
That is also true, I will try that tommorow. But I doubt that It will improve things to the level I want - I also tried some other things where you retrieve from Lists but so far nothing to say woow ...Strangely sometimes code works pretty fast, so I'm starting to wondering If there is maybe problem somewhere else - although no errors to be seen anywhere.
-
Feb 22nd, 2017, 01:14 AM
#13
Thread Starter
Fanatic Member
Re: Extracting extended file properties for Listview Items - slow code
I tried some of other solutions including list of strings, but nothing fast as Windows Explorer. So I ended up with this, my final code:
Code:
Public shell As New Shell
Public folders As Folder
Public files As FolderItem
folders = shell.[NameSpace](Path.GetDirectoryName(MyDirectory)
'Loop through files in directory
For each MyFiles As FileInfo In MyDirectory.EnumerateFiles
files = folders.ParseName(Path.GetFileName(MyFiles.FullName))
...
...
...
Listview1.Items.Add(LVI)
LVI.SubItems.Add(folder.GetDetailsOf(files, 2).Trim())
LVI.SubItems.Add(folder.GetDetailsOf(files, 1).Trim())
Next
I think I still have some reserves on how I add Items into Listview. By correcting that I might gain some more speed. Thanks for all your help si_the_geek !
-
Oct 4th, 2019, 10:18 AM
#14
Fanatic Member
Re: [RESOLVED] Extracting extended file properties for Listview Items - slow code
si,
In this codeline there are [] brackets. What does it mean and what is the correct syntax?
Is Fpath the name of the folder and the file?
Code:
Dim rFolder As Folder = shell.[NameSpace](Path.GetDirectoryName(Fpath))
Thanks
PK
-
Oct 4th, 2019, 11:44 AM
#15
Re: [RESOLVED] Extracting extended file properties for Listview Items - slow code
The square brackets are to allow you to use reserved words (such as the names of built-in functions) as the names of variables/properties etc.
So if you really wanted to, you could call a variable called dim:
Code:
Dim [dim] as Integer = 1
Console.WriteLine([dim])
...without the square brackets that wouldn't be possible.
The shell object seems to have a property called NameSpace, but as that is a reserved word the square brackets are needed... so what you quoted appears to be the correct syntax.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|