Results 1 to 9 of 9

Thread: [RESOLVED] [2008] Force a refresh of all Win Explorer windows showing a particular folder

  1. #1

    Thread Starter
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Resolved [RESOLVED] [2008] Force a refresh of all Win Explorer windows showing a particular folder

    The application I'm developing does some file management chores, such as renaming and deletion of files. I have noticed that often after a file is deleted via code, it is still shown in Windows Explorer although it doesn't exist anymore. A refresh (F5) solves this, however I wouldn't want end users to have to press F5 every couple of minutes when using the application.

    Can a refresh of all open Windows Explorer windows be requested via code? Moreover, can the refresh be requested only for windows displaying a particular location?

    I've thought of getting all open windows, filtering them and then sending the F5 keystroke, however it sounds a bit like overkill. Any easier way to do it?
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  2. #2
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2008] Force a refresh of all Win Explorer windows showing a particular folder

    Here you go. This should do it;

    Code:
        <DllImport("shell32.dll")> _
        Shared Sub SHChangeNotify( _
        ByVal wEventID As HChangeNotifyEventID, _
        ByVal uFlags As HChangeNotifyFlags, _
        ByVal dwItem1 As IntPtr, _
        ByVal dwItem2 As IntPtr)
        End Sub
    
        <Flags()> _
           Public Enum HChangeNotifyFlags
            SHCNF_DWORD = &H3
            SHCNF_IDLIST = &H0
            SHCNF_PATHA = &H1
            SHCNF_PATHW = &H5
            SHCNF_PRINTERA = &H2
            ' SHCNF_PRINTERW = &H6 
            SHCNF_FLUSH = &H1000
            SHCNF_FLUSHNOWAIT = &H2000
        End Enum
    
    
        <Flags()> _
           Public Enum HChangeNotifyEventID
            SHCNE_ALLEVENTS = &H7FFFFFFF
            SHCNE_ASSOCCHANGED = &H8000000
            SHCNE_ATTRIBUTES = &H800
            SHCNE_CREATE = &H2
            SHCNE_DELETE = &H4
            SHCNE_DRIVEADD = &H10
            SHCNE_DRIVEADDGUI = &H10000
            SHCNE_DRIVEREMOVED = &H80
            SHCNE_EXTENDED_EVENT = &H4000000
            SHCNE_FREESPACE = &H40000
            SHCNE_MEDIAINSERTED = &H20
            SHCNE_MEDIAREMOVED = &H40
            SHCNE_MKDIR = &H8
            SHCNE_NETSHARE = &H200
            SHCNE_NETUNSHARE = &H400
            SHCNE_RENAMEFOLDER = &H20000
            SHCNE_RENAMEITEM = &H1
            SHCNE_RMDIR = &H10
            SHCNE_SERVERDISCONNECT = &H4000
            SHCNE_UPDATEDIR = &H1000
            SHCNE_UPDATEIMAGE = &H8000
        End Enum
    Call it like this.
    Code:
            SHChangeNotify(HChangeNotifyEventID.SHCNE_ASSOCCHANGED, HChangeNotifyFlags.SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero)
    It will refresh the Explorer windows regardless of their current directory however, but good enough I think.

  3. #3

    Thread Starter
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [2008] Force a refresh of all Win Explorer windows showing a particular folder

    The refresh works fine for all Windows Explorer windows, however it doesn't refresh the desktop. This means that the issue persists for all files residing on the desktop. Any way to solve that?
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  4. #4
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2008] Force a refresh of all Win Explorer windows showing a particular folder

    Try this;

    Code:
    SHChangeNotify(&H8000000, &H0, 0, 0)
    If that does not work let me know, I have some other API code knocking around.
    Last edited by Bulldog; Feb 15th, 2009 at 05:17 PM.

  5. #5

    Thread Starter
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [2008] Force a refresh of all Win Explorer windows showing a particular folder

    Nope, no effect.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  6. #6
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2008] Force a refresh of all Win Explorer windows showing a particular folder

    Hmm.. that previous code works for me, in that case try;


    SHChangeNotify(&H8000000, &H1000, 0, 0)


    Another one is this;
    Code:
    Private Declare Function InvalidateRect Lib "user32" (ByVal hwnd As Long, lpRect As Long, ByVal bErase As Long) As Long
    
    'Call it as follows
    InvalidateRect( 0&, 0&, False)
    Last edited by Bulldog; Feb 16th, 2009 at 11:18 AM.

  7. #7

    Thread Starter
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [2008] Force a refresh of all Win Explorer windows showing a particular folder

    InvalidateRect will fail at runtime, False cannot be converted to Long like that. I will try the SHChangeNotify method and report back.

    I'm using Windows Vista if that is of any importance.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  8. #8
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2008] Force a refresh of all Win Explorer windows showing a particular folder

    Both

    SHChangeNotify(&H8000000, &H0, 0, 0)
    and
    SHChangeNotify(&H8000000, &H1000, 0, 0)

    work for me. The second is the more thorough however.

  9. #9

    Thread Starter
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [2008] Force a refresh of all Win Explorer windows showing a particular folder

    Ok I've just tried it a couple more times and the desktop was refreshed, but only sporadically. It seems that although the code by itself is ok, a refresh is sometimes prevented. However, it's good enough for now I think. Thank you for your help. Cheers!
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

Posting Permissions

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



Click Here to Expand Forum to Full Width