2 /* -----------------------------------------------------------------------
3 * <copyright file="NativeMethods.cs" company="GRNet">
5 * Copyright 2011-2012 GRNET S.A. All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
11 * 1. Redistributions of source code must retain the above
12 * copyright notice, this list of conditions and the following
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * The views and conclusions contained in the software and
35 * documentation are those of the authors and should not be
36 * interpreted as representing official policies, either expressed
37 * or implied, of GRNET S.A.
39 * -----------------------------------------------------------------------
43 using System.Collections.Generic;
46 using System.Runtime.InteropServices;
51 #region Enums & Structs
53 #region enum HChangeNotifyEventID
55 /// Describes the event that has occurred.
56 /// Typically, only one event is specified at a time.
57 /// If more than one event is specified, the values contained
58 /// in the <i>dwItem1</i> and <i>dwItem2</i>
59 /// parameters must be the same, respectively, for all specified events.
60 /// This parameter can be one or more of the following values.
63 /// <para><b>Windows NT/2000/XP:</b> <i>dwItem2</i> contains the index
64 /// in the system image list that has changed.
65 /// <i>dwItem1</i> is not used and should be <see langword="null"/>.</para>
66 /// <para><b>Windows 95/98:</b> <i>dwItem1</i> contains the index
67 /// in the system image list that has changed.
68 /// <i>dwItem2</i> is not used and should be <see langword="null"/>.</para>
71 public enum HChangeNotifyEventID
74 /// All events have occurred.
76 SHCNE_ALLEVENTS = 0x7FFFFFFF,
79 /// A file type association has changed. <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/>
80 /// must be specified in the <i>uFlags</i> parameter.
81 /// <i>dwItem1</i> and <i>dwItem2</i> are not used and must be <see langword="null"/>.
83 SHCNE_ASSOCCHANGED = 0x08000000,
86 /// The attributes of an item or folder have changed.
87 /// <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or
88 /// <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>.
89 /// <i>dwItem1</i> contains the item or folder that has changed.
90 /// <i>dwItem2</i> is not used and should be <see langword="null"/>.
92 SHCNE_ATTRIBUTES = 0x00000800,
95 /// A nonfolder item has been created.
96 /// <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or
97 /// <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>.
98 /// <i>dwItem1</i> contains the item that was created.
99 /// <i>dwItem2</i> is not used and should be <see langword="null"/>.
101 SHCNE_CREATE = 0x00000002,
104 /// A nonfolder item has been deleted.
105 /// <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or
106 /// <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>.
107 /// <i>dwItem1</i> contains the item that was deleted.
108 /// <i>dwItem2</i> is not used and should be <see langword="null"/>.
110 SHCNE_DELETE = 0x00000004,
113 /// A drive has been added.
114 /// <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or
115 /// <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>.
116 /// <i>dwItem1</i> contains the root of the drive that was added.
117 /// <i>dwItem2</i> is not used and should be <see langword="null"/>.
119 SHCNE_DRIVEADD = 0x00000100,
122 /// A drive has been added and the Shell should create a new window for the drive.
123 /// <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or
124 /// <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>.
125 /// <i>dwItem1</i> contains the root of the drive that was added.
126 /// <i>dwItem2</i> is not used and should be <see langword="null"/>.
128 SHCNE_DRIVEADDGUI = 0x00010000,
131 /// A drive has been removed. <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or
132 /// <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>.
133 /// <i>dwItem1</i> contains the root of the drive that was removed.
134 /// <i>dwItem2</i> is not used and should be <see langword="null"/>.
136 SHCNE_DRIVEREMOVED = 0x00000080,
139 /// Not currently used.
141 SHCNE_EXTENDED_EVENT = 0x04000000,
144 /// The amount of free space on a drive has changed.
145 /// <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or
146 /// <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>.
147 /// <i>dwItem1</i> contains the root of the drive on which the free space changed.
148 /// <i>dwItem2</i> is not used and should be <see langword="null"/>.
150 SHCNE_FREESPACE = 0x00040000,
153 /// Storage media has been inserted into a drive.
154 /// <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or
155 /// <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>.
156 /// <i>dwItem1</i> contains the root of the drive that contains the new media.
157 /// <i>dwItem2</i> is not used and should be <see langword="null"/>.
159 SHCNE_MEDIAINSERTED = 0x00000020,
162 /// Storage media has been removed from a drive.
163 /// <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or
164 /// <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>.
165 /// <i>dwItem1</i> contains the root of the drive from which the media was removed.
166 /// <i>dwItem2</i> is not used and should be <see langword="null"/>.
168 SHCNE_MEDIAREMOVED = 0x00000040,
171 /// A folder has been created. <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/>
172 /// or <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>.
173 /// <i>dwItem1</i> contains the folder that was created.
174 /// <i>dwItem2</i> is not used and should be <see langword="null"/>.
176 SHCNE_MKDIR = 0x00000008,
179 /// A folder on the local computer is being shared via the network.
180 /// <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or
181 /// <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>.
182 /// <i>dwItem1</i> contains the folder that is being shared.
183 /// <i>dwItem2</i> is not used and should be <see langword="null"/>.
185 SHCNE_NETSHARE = 0x00000200,
188 /// A folder on the local computer is no longer being shared via the network.
189 /// <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or
190 /// <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>.
191 /// <i>dwItem1</i> contains the folder that is no longer being shared.
192 /// <i>dwItem2</i> is not used and should be <see langword="null"/>.
194 SHCNE_NETUNSHARE = 0x00000400,
197 /// The name of a folder has changed.
198 /// <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or
199 /// <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>.
200 /// <i>dwItem1</i> contains the previous pointer to an item identifier list (PIDL) or name of the folder.
201 /// <i>dwItem2</i> contains the new PIDL or name of the folder.
203 SHCNE_RENAMEFOLDER = 0x00020000,
206 /// The name of a nonfolder item has changed.
207 /// <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or
208 /// <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>.
209 /// <i>dwItem1</i> contains the previous PIDL or name of the item.
210 /// <i>dwItem2</i> contains the new PIDL or name of the item.
212 SHCNE_RENAMEITEM = 0x00000001,
215 /// A folder has been removed.
216 /// <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or
217 /// <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>.
218 /// <i>dwItem1</i> contains the folder that was removed.
219 /// <i>dwItem2</i> is not used and should be <see langword="null"/>.
221 SHCNE_RMDIR = 0x00000010,
224 /// The computer has disconnected from a server.
225 /// <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or
226 /// <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>.
227 /// <i>dwItem1</i> contains the server from which the computer was disconnected.
228 /// <i>dwItem2</i> is not used and should be <see langword="null"/>.
230 SHCNE_SERVERDISCONNECT = 0x00004000,
233 /// The contents of an existing folder have changed,
234 /// but the folder still exists and has not been renamed.
235 /// <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or
236 /// <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>.
237 /// <i>dwItem1</i> contains the folder that has changed.
238 /// <i>dwItem2</i> is not used and should be <see langword="null"/>.
239 /// If a folder has been created, deleted, or renamed, use SHCNE_MKDIR, SHCNE_RMDIR, or
240 /// SHCNE_RENAMEFOLDER, respectively, instead.
242 SHCNE_UPDATEDIR = 0x00001000,
244 SHCNE_UPDATEITEM = 0x00002000,
247 /// An image in the system image list has changed.
248 /// <see cref="HChangeNotifyFlags.SHCNF_DWORD"/> must be specified in <i>uFlags</i>.
250 SHCNE_UPDATEIMAGE = 0x00008000,
253 #endregion // enum HChangeNotifyEventID
255 #region public enum HChangeNotifyFlags
257 /// Flags that indicate the meaning of the <i>dwItem1</i> and <i>dwItem2</i> parameters.
258 /// The uFlags parameter must be one of the following values.
261 public enum HChangeNotifyFlags
264 /// The <i>dwItem1</i> and <i>dwItem2</i> parameters are DWORD values.
266 SHCNF_DWORD = 0x0003,
268 /// <i>dwItem1</i> and <i>dwItem2</i> are the addresses of ITEMIDLIST structures that
269 /// represent the item(s) affected by the change.
270 /// Each ITEMIDLIST must be relative to the desktop folder.
272 SHCNF_IDLIST = 0x0000,
274 /// <i>dwItem1</i> and <i>dwItem2</i> are the addresses of null-terminated strings of
275 /// maximum length MAX_PATH that contain the full path names
276 /// of the items affected by the change.
278 SHCNF_PATHA = 0x0001,
280 /// <i>dwItem1</i> and <i>dwItem2</i> are the addresses of null-terminated strings of
281 /// maximum length MAX_PATH that contain the full path names
282 /// of the items affected by the change.
284 SHCNF_PATHW = 0x0005,
286 /// <i>dwItem1</i> and <i>dwItem2</i> are the addresses of null-terminated strings that
287 /// represent the friendly names of the printer(s) affected by the change.
289 SHCNF_PRINTERA = 0x0002,
291 /// <i>dwItem1</i> and <i>dwItem2</i> are the addresses of null-terminated strings that
292 /// represent the friendly names of the printer(s) affected by the change.
294 SHCNF_PRINTERW = 0x0006,
296 /// The function should not return until the notification
297 /// has been delivered to all affected components.
298 /// As this flag modifies other data-type flags, it cannot by used by itself.
300 SHCNF_FLUSH = 0x1000,
302 /// The function should begin delivering notifications to all affected components
303 /// but should return as soon as the notification process has begun.
304 /// As this flag modifies other data-type flags, it cannot by used by itself.
306 SHCNF_FLUSHNOWAIT = 0x2000
308 #endregion // enum HChangeNotifyFlags
313 public static class NativeMethods
315 [DllImport("shell32.dll")]
316 public static extern void SHChangeNotify(HChangeNotifyEventID wEventId,
317 HChangeNotifyFlags uFlags,
321 public static void RaiseChangeNotification(string path)
323 if (String.IsNullOrWhiteSpace(path))
324 throw new ArgumentNullException("path", "The path parameter must not be emtpy");
326 if (!Directory.Exists(path) && !File.Exists(path))
330 IntPtr pathPointer = Marshal.StringToCoTaskMemAuto(path);
334 NativeMethods.SHChangeNotify(HChangeNotifyEventID.SHCNE_UPDATEITEM,
335 HChangeNotifyFlags.SHCNF_PATHW | HChangeNotifyFlags.SHCNF_FLUSHNOWAIT,
336 pathPointer, IntPtr.Zero);
340 Marshal.FreeHGlobal(pathPointer);