dcsimg
Results 1 to 19 of 19

Thread: [.NET 3.5+] ShellFile - Managed Wrapper To Retrieve Extended Properties

  1. #1

    Thread Starter
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    [.NET 3.5+] ShellFile - Managed Wrapper To Retrieve Extended Properties

    This was created as a response for this thread. Basically, it is a way to read (not write) all those extra properties that Windows has. This code requires a reference to "Microsoft Shell Controls And Automation" which is under the COM tab.

    You also need to add the ShellFile.vb file to your project.

    In the example I will retrieve the frame width of a video:

    Name:  details.png
Views: 3941
Size:  30.8 KB

    Code:

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles Button1.Click
    
            '//create the wrapped file by passing in a path to a file
            Using file = New ShellFile("C:\Users\Public\Videos\Sample Videos\Wildlife.wmv")
    
                '//access the extended property by name (case insensitive search)
                Dim frameWidth = file.ExtendedProperties("frame width")
    
                '//check for nothing because if you attempt to index a property
                '//that doesn't exist then it will return null (nothing)
                If frameWidth IsNot Nothing Then
                    MessageBox.Show(frameWidth.Value)
                End If
            End Using
        End Sub
    
    End Class
    Output:

    Name:  output.png
Views: 3616
Size:  6.3 KB

    You can refer to this to see a relatively current list of valid extended properties by operating system. Or, you can just output all the extended property names that the code produces.

    This code was tested on x64 Windows 7 Home Premium. Code contains Xml commenting as well.
    Last edited by ForumAccount; Jul 22nd, 2011 at 11:02 AM. Reason: Applied different styling

  2. #2
    Junior Member
    Join Date
    Feb 2009
    Posts
    31

    Re: [.NET 3.5+] ShellFile - Managed Wrapper To Retrieve Extended Properties

    Hello

    I have looked at your post and I am trying it now

    What is the shellfile defind as?? I am getting an error even though I have imported the Microsoft Shell in the Coms

    thanks

  3. #3

    Thread Starter
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: [.NET 3.5+] ShellFile - Managed Wrapper To Retrieve Extended Properties

    Did you add the ShellFile.vb to your project?

  4. #4
    Junior Member
    Join Date
    Feb 2009
    Posts
    31

    Re: [.NET 3.5+] ShellFile - Managed Wrapper To Retrieve Extended Properties

    I have now .... oops

    Great work BTW been looking to access this info for ages

    Thanks

  5. #5
    Addicted Member
    Join Date
    Jun 2008
    Location
    Macedonia
    Posts
    188

    Re: [.NET 3.5+] ShellFile - Managed Wrapper To Retrieve Extended Properties

    Nice code finaly i found.
    Last edited by stru4nak; Jul 20th, 2011 at 11:47 AM.
    MACEDONIA 4EVER

  6. #6

    Thread Starter
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: [.NET 3.5+] ShellFile - Managed Wrapper To Retrieve Extended Properties

    Read the sentence in my first post that has bolded words in it.

  7. #7
    New Member
    Join Date
    Aug 2012
    Location
    Atlanta
    Posts
    2

    Re: [.NET 3.5+] ShellFile - Managed Wrapper To Retrieve Extended Properties

    The use of this shellfile.vb is great. There is another article that shows how to read and write (get and set) the properties at http://www.codeproject.com/Articles/...m-Managed-Code. Slight changes need to be made to the mentioned dll files. These changes are noted in the replies for other users. It works great after the changes. Happy coding...

  8. #8
    Member
    Join Date
    Oct 2012
    Posts
    35

    Re: [.NET 3.5+] ShellFile - Managed Wrapper To Retrieve Extended Properties

    Hi, I'm trying to implement this in a project I'm working on but it doesn't seem to work? I get the error message "Object reference not set to an instance of an object."
    I've imported ShellFile.vb into my project and the code I am running is the same as the example you have put, however I have changed the video path.
    Any suggestions?

  9. #9

    Thread Starter
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: [.NET 3.5+] ShellFile - Managed Wrapper To Retrieve Extended Properties

    Quote Originally Posted by LukeVoyager View Post
    Hi, I'm trying to implement this in a project I'm working on but it doesn't seem to work? I get the error message "Object reference not set to an instance of an object."
    I've imported ShellFile.vb into my project and the code I am running is the same as the example you have put, however I have changed the video path.
    Any suggestions?
    Please provide the stack trace of the error message.

  10. #10
    Member
    Join Date
    Oct 2012
    Posts
    35

    Re: [.NET 3.5+] ShellFile - Managed Wrapper To Retrieve Extended Properties

    How do I do this?
    Sorry I have never had an error that's needed a stack trace to handle it. I've tried putting Msgbox(System.Environment.StackTrace) but that is before exception.
    The exception is happening on the code "Dim frameWidth = file.ExtendedProperties("Frame width")" and I have to put Msgbox(System.Environment.StackTrace) before this other wise I get no message box.
    Do you want the output of that?

    EDIT: Sorry I have just figured it out :P

    System.NullReferenceException was unhandled
    Message=Object reference not set to an instance of an object.
    Source=videoScan
    StackTrace:
    at videoScan.Form1.Button1_Click(Object sender, EventArgs e) in D:\Docs\Visual Basic\videoScan\videoScan\Form1.vb:line 12
    at System.Windows.Forms.Control.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
    at System.Windows.Forms.Control.WndProc(Message& m)
    at System.Windows.Forms.ButtonBase.WndProc(Message& m)
    at System.Windows.Forms.Button.WndProc(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
    at videoScan.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Threading.ThreadHelper.ThreadStart()
    InnerException:

  11. #11

    Thread Starter
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: [.NET 3.5+] ShellFile - Managed Wrapper To Retrieve Extended Properties

    No, wrap the code in a Try Catch Block and place a breakpoint on the Catch clause. Drill into the exception properties (View Details) and retrieve that stacktrace. You can also go to Debug > Breakpoints > Common Language Runtime Exceptions and ensure that Thrown is checked.

  12. #12
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,243

    Re: [.NET 3.5+] ShellFile - Managed Wrapper To Retrieve Extended Properties

    Quote Originally Posted by LukeVoyager View Post
    ........
    however I have changed the video path.
    Any suggestions?
    Are you sure the path is correct? I get a very similar error if the file does not exist. May not be your problem, but worth checking.

  13. #13
    Member
    Join Date
    Oct 2012
    Posts
    35

    Re: [.NET 3.5+] ShellFile - Managed Wrapper To Retrieve Extended Properties

    This is the only thing that was under Stacktrace -> at videoScan.Form1.Button1_Click(Object sender, EventArgs e) in D:\Docs\Visual Basic\videoScan\videoScan\Form1.vb:line 12
    Got there by clicking View Details>system.NullReferenceExecption>Stacktrace.

    Just checked the path and it's fine, thanks for the suggestion

    EDIT: If it helps, the error occurs on this line -> Dim frameWidth = file.ExtendedProperties("Frame width")
    Last edited by LukeVoyager; Jan 16th, 2013 at 04:57 AM.

  14. #14
    New Member
    Join Date
    Mar 2013
    Posts
    3

    Re: [.NET 3.5+] ShellFile - Managed Wrapper To Retrieve Extended Properties

    I'm getting nothing !

    I tested a lot of propertys (name, size, etc...), I always get "nothing" !

    This is the code:

    Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Using file = New ShellFile("C:\Users\Administrador\Downloads\videos\convertir\escapada.avi")
    Dim name = file.ExtendedProperties("Name")
    If name IsNot Nothing Then
    MessageBox.Show(name.Value)
    Else
    MsgBox("nothing")
    End If
    End Using
    End Sub
    End Class

    Why I can't get any value?

    Maybe the "shellFile" class needs a specific Windows service running or something?

  15. #15
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: [.NET 3.5+] ShellFile - Managed Wrapper To Retrieve Extended Properties

    For those people who are having problems getting it to return any values - you may not be showing file extensions in Windows Explorer. The ShellFile.vb GetCollection procedure is trying to compare "Picture1.jpg" to "Picture1" - which don't match, and no extended properties are returned.

  16. #16
    New Member
    Join Date
    Jun 2016
    Posts
    4

    Re: [.NET 3.5+] ShellFile - Managed Wrapper To Retrieve Extended Properties

    Quote Originally Posted by bushmobile View Post
    For those people who are having problems getting it to return any values - you may not be showing file extensions in Windows Explorer. The ShellFile.vb GetCollection procedure is trying to compare "Picture1.jpg" to "Picture1" - which don't match, and no extended properties are returned.
    Thanks for the blog it was very helpful

  17. #17
    New Member
    Join Date
    Jul 2016
    Posts
    1

    Re: [.NET 3.5+] ShellFile - Managed Wrapper To Retrieve Extended Properties

    In VB.net 2010 on Win 7 Enterprise the values seem to have a hidden character at the beginning.

    Example:
    I need the frame rate for a calculation but in the code below, fr is always zero:
    Code:
                        ' Get frame rate 
                        Dim fr As Double
                        Dim fps = file.ExtendedProperties("frame rate")
                        If fps IsNot Nothing AndAlso fps.Value <> "" Then
                            fr = Val(fps.Value)
                            TextBox_Feedback.AppendText("Frame rate: " + vbTab + fr.ToString + " frames/sec" + vbCrLf)
                            ' ### Debug
                            Dim tmpStr As String
                            tmpStr = fps.Value
                            Debug.Print(tmpStr + "; " + Str2Hex(tmpStr, True))
                            ' ### End debug
                        Else
                            TextBox_Feedback.AppendText("Frame rate: " + vbTab + "Unknown" + vbCrLf)
                        End If
    'Str2Hex' produces the ASCII codes, in Hex, for the characters in the input string (borrowed from one of my other projects for debugging purposes here).

    The feedback text box shows: "Frame rate: 0 frames/sec"
    and the immediate window shows: "23 frames/second; 3F 32 33 20 66 72 61 6D 65 73 2F 73 65 63 6F 6E 64"

    So it looks like fps.value returns "23 frames/second" but in reality there is a hidden ASCII 3F (should be shown as '?') at the beginning and that makes Val return zero every time.

    I can get the correct value (on this machine) by discarding the first character:
    Code:
    fr = Val(Mid(fps.Value,2))
    Will Windows always include that hidden character? Or will the fixed code produce the wrong value on other versions?

  18. #18
    New Member
    Join Date
    Jun 2016
    Posts
    4

    Re: [.NET 3.5+] ShellFile - Managed Wrapper To Retrieve Extended Properties

    Hello,

    First , thanks for this post it was very usefull to me

    I used it in my application and it worked fine,but when I tried to run my application on another computer
    I got this Eror:
    Unable to cast COM object of type 'Shell32.ShellClass' to interface type 'Shell32.IShellDispatch6'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{286E6F1B-7113-4355-9562-96B7E9D64C54}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

    Do you know somthing about this??
    Please help me, it very urgent to me
    Thanks in advance

  19. #19
    New Member
    Join Date
    Feb 2017
    Posts
    5

    Re: [.NET 3.5+] ShellFile - Managed Wrapper To Retrieve Extended Properties

    Quote Originally Posted by dina123456 View Post
    Hello,

    First , thanks for this post it was very usefull to me

    I used it in my application and it worked fine,but when I tried to run my application on another computer
    I got this Eror:
    Unable to cast COM object of type 'Shell32.ShellClass' to interface type 'Shell32.IShellDispatch6'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{286E6F1B-7113-4355-9562-96B7E9D64C54}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

    Do you know somthing about this??
    Please help me, it very urgent to me
    Thanks in advance
    I have the same error. Anyone knows how to fix this?

    Thanks

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width