Click here to Skip to main content
12,623,511 members (30,538 online)
Click here to Skip to main content
Add your own
alternative version

Stats

54.7K views
24 bookmarked
Posted

Programmatic Refresh of a File System

, 17 Apr 2005
Rate this:
Please Sign up or sign in to vote.
How to force the file system to refresh.

Introduction

This is a short article, written after many hours spent on the problem of refreshing a CD file system after burning. At the end, the solution I've found is very very simple, so I've decided to explain it here.

The function

I've used a call to the BroadcastSystemMessage function to send a WM_DEVICECHANGE message to Windows (XP in my case). The function is defined as follows (found on MSDN):

long BroadcastSystemMessage(      
    DWORD dwFlags,
    LPDWORD lpdwRecipients,
    UINT uiMessage,
    WPARAM wParam,
    LPARAM lParam
);

To set the correct parameters for the call, I've initialized a DEV_BROADCAST_VOLUME struct, which is defined as:

typedef struct _DEV_BROADCAST_VOLUME {
  DWORD dbcv_size;
  DWORD dbcv_devicetype;
  DWORD dbcv_reserved;
  DWORD dbcv_unitmask;
  WORD dbcv_flags;
} DEV_BROADCAST_VOLUME, *PDEV_BROADCAST_VOLUME;

The code of my function is here:

bool RefreshFileSystem(char cdrive)
{
  DEV_BROADCAST_VOLUME hdr;
  long result;

  hdr.dbcv_size       =  sizeof(DEV_BROADCAST_VOLUME);
  hdr.dbcv_devicetype =  DBT_DEVTYP_VOLUME;
  hdr.dbcv_reserved = 0;
  cdrive = (char)_totupper(cdrive));  // thanks to Blake Miller
  hdr.dbcv_unitmask = 1 << (cdrive-'A');
  hdr.dbcv_flags  = DBTF_MEDIA;

  result = BroadcastSystemMessage(BSF_NOHANG|BSF_POSTMESSAGE,
                         NULL, WM_DEVICECHANGE, DBT_DEVICEARRIVAL,
                         (LPARAM)&hdr);
  return (result>=0);
}

I've heard of someone who used this kind of a solution but unfortunately the system crashed. I'm trying to find out what has caused the crash.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

shaman74
Web Developer
Italy Italy
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralC# code - Works for me on network drives Pin
theit851412-Jun-11 18:00
membertheit851412-Jun-11 18:00 
Had to find a way to refresh the user's my computer windows to show drive mapping updates...
Here is my C# code:
/// <summary>
/// Changed for C# and to allow for network drive types.
/// </summary>
/// <param name="localDrive"></param>
public static void RefreshDrive(string localDrive)
{
    DEV_BROADCAST_VOLUME hdr = new DEV_BROADCAST_VOLUME();
 
    // collect and clean the passed LocalDrive param
    if (localDrive == null || localDrive.Length == 0)
        throw new System.Exception("Invalid 'localDrive' passed, 'localDrive' parameter cannot be 'empty'");
    char drive = localDrive[0];
    long result;
 
    hdr.dbcv_size = (uint)Marshal.SizeOf(hdr);
    hdr.dbcv_devicetype = (uint)DevType.DBT_DEVTYP_NET;
    hdr.dbcv_reserved = 0;
    hdr.dbcv_unitmask = (uint) 1 << (drive - 'A');
    hdr.dbcv_flags = (ushort)Dbtf.DBTF_NET;
 
    IntPtr hdrp = Marshal.AllocHGlobal(Marshal.SizeOf(hdr));
    Marshal.StructureToPtr(hdr, hdrp, true);
    MessageBroadcastRecipients recip = MessageBroadcastRecipients.BSM_ALLCOMPONENTS;
 
    result = BroadcastSystemMessage(MessageBroadcastFlags.BSF_NOHANG | MessageBroadcastFlags.BSF_POSTMESSAGE,
                           ref recip, WM_DEVICECHANGE, (IntPtr)DBT_DEVICEARRIVAL, hdrp);
 
    Marshal.FreeHGlobal(hdrp);
}
 
[DllImport("user32", SetLastError = true)]
private static extern int BroadcastSystemMessage(MessageBroadcastFlags flags, ref MessageBroadcastRecipients lpInfo, uint Msg, IntPtr wParam, IntPtr lParam);
 
const uint WM_DEVICECHANGE = 0x0219;
const uint DBT_DEVICEARRIVAL = 0x8000;
 
enum DevType : uint
{
    DBT_DEVTYP_OEM = 0,
    DBT_DEVTYP_DEVNODE = 1,
    DBT_DEVTYP_VOLUME = 2,
    DBT_DEVTYP_PORT = 3,
    DBT_DEVTYP_NET = 4,
}
enum Dbtf : uint
{
    DBTF_MEDIA = 0x0001,
    DBTF_NET = 0x0002,
}
 
[Flags]
enum MessageBroadcastFlags : uint
{
    BSF_QUERY = 0x00000001,
    BSF_IGNORECURRENTTASK = 0x00000002,
    BSF_FLUSHDISK = 0x00000004,
    BSF_NOHANG = 0x00000008,
    BSF_POSTMESSAGE = 0x00000010,
    BSF_FORCEIFHUNG = 0x00000020,
    BSF_NOTIMEOUTIFNOTHUNG = 0x00000040,
    BSF_ALLOWSFW = 0x00000080,
    BSF_SENDNOTIFYMESSAGE = 0x00000100,
    BSF_RETURNHDESK = 0x00000200,
    BSF_LUID = 0x00000400
}
[Flags]
enum MessageBroadcastRecipients : uint
{
    BSM_ALLCOMPONENTS = 0x00000000,
    BSM_VXDS = 0x00000001,
    BSM_NETDRIVER = 0x00000002,
    BSM_INSTALLABLEDRIVERS = 0x00000004,
    BSM_APPLICATIONS = 0x00000008,
    BSM_ALLDESKTOPS = 0x00000010
}
 
[StructLayout(LayoutKind.Sequential)]
public struct DEV_BROADCAST_VOLUME
{
    public uint dbcv_size;
    public uint dbcv_devicetype;
    public uint dbcv_reserved;
    public uint dbcv_unitmask;
    public ushort dbcv_flags;
}

If you're unmapping a drive and remapping it to a different location, you must call RefreshDrive after the unmap and then after the mapping. If you do just one, the drive map may disappear or still show the old name.

// Unmap the existing drive, if it exists.
UnmapDrive(Drive);
NetworkDrive.RefreshDrive(Drive);
Thread.Sleep(1000);
MapDrive(Drive, uncpath);
NetworkDrive.RefreshDrive(Drive);

GeneralPossible crash causess Pin
Simon Owen25-Jun-06 22:58
memberSimon Owen25-Jun-06 22:58 
Questionquery Pin
achalshah717-Mar-06 2:52
memberachalshah717-Mar-06 2:52 
GeneralSHChangeNotify Pin
Cabbi9-Jun-05 3:53
memberCabbi9-Jun-05 3:53 
Generalcan't refresh CDRW content Pin
Sergey Apollonov23-May-05 20:34
sussSergey Apollonov23-May-05 20:34 
GeneralCode Correction : Drive Letter Pin
Blake Miller12-Apr-05 5:30
memberBlake Miller12-Apr-05 5:30 
GeneralRe: Code Correction : Drive Letter Pin
shaman7412-Apr-05 6:17
membershaman7412-Apr-05 6:17 
GeneralHhhhhhhhhhhhhhh ! Pin
Kochise8-Apr-05 0:24
memberKochise8-Apr-05 0:24 
GeneralRe: Hhhhhhhhhhhhhhh ! Pin
shaman748-Apr-05 0:32
membershaman748-Apr-05 0:32 
GeneralRe: Hhhhhhhhhhhhhhh ! Pin
Outlook14-Apr-05 11:41
memberOutlook14-Apr-05 11:41 
GeneralRe: Hhhhhhhhhhhhhhh ! Pin
shaman7414-Apr-05 21:52
membershaman7414-Apr-05 21:52 
GeneralRe: Hhhhhhhhhhhhhhh ! Pin
MX-Mauro18-Apr-05 2:20
memberMX-Mauro18-Apr-05 2:20 
GeneralRe: Hhhhhhhhhhhhhhh ! Pin
Andy N18-Apr-05 14:15
memberAndy N18-Apr-05 14:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161128.1 | Last Updated 18 Apr 2005
Article Copyright 2005 by shaman74
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid