La mia app di esempio si blocca durante la registrazione alla notifica di Filechangeinfo

public partial class Form1 : Form { [DllImport("coredll.dll")] static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong); [DllImport("coredll.dll")] static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam); [DllImport("coredll.dll")] public static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex); //public struct tagSHCHANGENOTIFYENTRY //{ // [MarshalAs(UnmanagedType.SysUInt)] // public ulong dwEventMask; // [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4096)] // public string WatchDir; // [MarshalAs(UnmanagedType.Bool)] // public bool fRecursive; //} //tagSHCHANGENOTIFYENTRY test; //[DllImport("aygshell.dll")] //static extern bool SHChangeNotifyRegister(IntPtr hwnd, ref tagSHCHANGENOTIFYENTRY test); const int GWL_WNDPROC = -4; public delegate int WindProc(IntPtr hWnd, int msg, IntPtr Wparam, IntPtr lparam); static private WindProc SampleProc; IntPtr OldDefProc = IntPtr.Zero; public enum SHCNE : uint { SHCNE_RENAMEITEM = 0x00000001, SHCNE_CREATE = 0x00000002, SHCNE_DELETE = 0x00000004, SHCNE_MKDIR = 0x00000008, SHCNE_RMDIR = 0x00000010, SHCNE_MEDIAINSERTED = 0x00000020, SHCNE_MEDIAREMOVED = 0x00000040, SHCNE_DRIVEREMOVED = 0x00000080, SHCNE_DRIVEADD = 0x00000100, SHCNE_NETSHARE = 0x00000200, SHCNE_NETUNSHARE = 0x00000400, SHCNE_ATTRIBUTES = 0x00000800, SHCNE_UPDATEDIR = 0x00001000, SHCNE_UPDATEITEM = 0x00002000, SHCNE_SERVERDISCONNECT = 0x00004000, SHCNE_UPDATEIMAGE = 0x00008000, SHCNE_DRIVEADDGUI = 0x00010000, SHCNE_RENAMEFOLDER = 0x00020000, SHCNE_FREESPACE = 0x00040000, SHCNE_EXTENDED_EVENT = 0x04000000, SHCNE_ASSOCCHANGED = 0x08000000, SHCNE_DISKEVENTS = 0x0002381F, SHCNE_GLOBALEVENTS = 0x0C0581E0, SHCNE_ALLEVENTS = 0x7FFFFFFF, SHCNE_INTERRUPT = 0x80000000, } public enum SHCNF { SHCNF_IDLIST = 0x0000, SHCNF_PATHA = 0x0001, SHCNF_PRINTERA = 0x0002, SHCNF_DWORD = 0x0003, SHCNF_PATHW = 0x0005, SHCNF_PRINTERW = 0x0006, SHCNF_TYPE = 0x00FF, SHCNF_FLUSH = 0x1000, SHCNF_FLUSHNOWAIT = 0x2000 } public const uint WM_SHNOTIFY = 0x0401; private const int WM_FILECHANGEINFO = (0x8000 + 0x101); public struct SHChangeNotifyEntry { public IntPtr pIdl; [MarshalAs(UnmanagedType.Bool)] public Boolean Recursively; } [DllImport("coredll.dll", EntryPoint = "#2", CharSet = CharSet.Auto)] private static extern uint SHChangeNotifyRegister( IntPtr hWnd, SHCNF fSources, SHCNE fEvents, uint wMsg, int cEntries, ref SHChangeNotifyEntry pFsne); [DllImport("Ceshell.dll", CharSet = CharSet.Auto)] private static extern uint SHGetSpecialFolderLocation( IntPtr hWnd, CSIDL nFolder, out IntPtr Pidl); public enum CSIDL { ///  /// Desktop ///  CSIDL_DESKTOP = 0x0000, ///  /// Internet Explorer (icon on desktop) ///  CSIDL_INTERNET = 0x0001, ///  /// Start Menu\Programs ///  CSIDL_PROGRAMS = 0x0002, ///  /// My Computer\Control Panel ///  CSIDL_CONTROLS = 0x0003, ///  /// My Computer\Printers ///  CSIDL_PRINTERS = 0x0004, ///  /// My Documents ///  CSIDL_PERSONAL = 0x0005, ///  /// user name\Favorites ///  CSIDL_FAVORITES = 0x0006, ///  /// Start Menu\Programs\Startup ///  CSIDL_STARTUP = 0x0007, ///  /// user name\Recent ///  CSIDL_RECENT = 0x0008, ///  /// user name\SendTo ///  CSIDL_SENDTO = 0x0009, ///  /// desktop\Recycle Bin ///  CSIDL_BITBUCKET = 0x000a, ///  /// user name\Start Menu ///  CSIDL_STARTMENU = 0x000b, ///  /// logical "My Documents" desktop icon ///  CSIDL_MYDOCUMENTS = 0x000c, ///  /// "My Music" folder ///  CSIDL_MYMUSIC = 0x000d, ///  /// "My Videos" folder ///  CSIDL_MYVIDEO = 0x000e, ///  /// user name\Desktop ///  CSIDL_DESKTOPDIRECTORY = 0x0010, ///  /// My Computer ///  CSIDL_DRIVES = 0x0011, ///  /// Network Neighborhood (My Network Places) ///  CSIDL_NETWORK = 0x0012, ///  /// user name>nethood ///  CSIDL_NETHOOD = 0x0013, ///  /// windows\fonts ///  CSIDL_FONTS = 0x0014, CSIDL_TEMPLATES = 0x0015, ///  /// All Users\Start Menu ///  CSIDL_COMMON_STARTMENU = 0x0016, ///  /// All Users\Start Menu\Programs ///  CSIDL_COMMON_PROGRAMS = 0X0017, ///  /// All Users\Startup ///  CSIDL_COMMON_STARTUP = 0x0018, ///  /// All Users\Desktop ///  CSIDL_COMMON_DESKTOPDIRECTORY = 0x0019, ///  /// user name\Application Data ///  CSIDL_APPDATA = 0x001a, ///  /// user name\PrintHood ///  CSIDL_PRINTHOOD = 0x001b, ///  /// user name\Local Settings\Applicaiton Data (non roaming) ///  CSIDL_LOCAL_APPDATA = 0x001c, ///  /// non localized startup ///  CSIDL_ALTSTARTUP = 0x001d, ///  /// non localized common startup ///  CSIDL_COMMON_ALTSTARTUP = 0x001e, CSIDL_COMMON_FAVORITES = 0x001f, CSIDL_INTERNET_CACHE = 0x0020, CSIDL_COOKIES = 0x0021, CSIDL_HISTORY = 0x0022, ///  /// All Users\Application Data ///  CSIDL_COMMON_APPDATA = 0x0023, ///  /// GetWindowsDirectory() ///  CSIDL_WINDOWS = 0x0024, ///  /// GetSystemDirectory() ///  CSIDL_SYSTEM = 0x0025, ///  /// C:\Program Files ///  CSIDL_PROGRAM_FILES = 0x0026, ///  /// C:\Program Files\My Pictures ///  CSIDL_MYPICTURES = 0x0027, ///  /// USERPROFILE ///  CSIDL_PROFILE = 0x0028, ///  /// x86 system directory on RISC ///  CSIDL_SYSTEMX86 = 0x0029, ///  /// x86 C:\Program Files on RISC ///  CSIDL_PROGRAM_FILESX86 = 0x002a, ///  /// C:\Program Files\Common ///  CSIDL_PROGRAM_FILES_COMMON = 0x002b, ///  /// x86 Program Files\Common on RISC ///  CSIDL_PROGRAM_FILES_COMMONX86 = 0x002c, ///  /// All Users\Templates ///  CSIDL_COMMON_TEMPLATES = 0x002d, ///  /// All Users\Documents ///  CSIDL_COMMON_DOCUMENTS = 0x002e, ///  /// All Users\Start Menu\Programs\Administrative Tools ///  CSIDL_COMMON_ADMINTOOLS = 0x002f, ///  /// user name\Start Menu\Programs\Administrative Tools ///  CSIDL_ADMINTOOLS = 0x0030, ///  /// Network and Dial-up Connections ///  CSIDL_CONNECTIONS = 0x0031, ///  /// All Users\My Music ///  CSIDL_COMMON_MUSIC = 0x0035, ///  /// All Users\My Pictures ///  CSIDL_COMMON_PICTURES = 0x0036, ///  /// All Users\My Video ///  CSIDL_COMMON_VIDEO = 0x0037, ///  /// Resource Direcotry ///  CSIDL_RESOURCES = 0x0038, ///  /// Localized Resource Direcotry ///  CSIDL_RESOURCES_LOCALIZED = 0x0039, ///  /// Links to All Users OEM specific apps ///  CSIDL_COMMON_OEM_LINKS = 0x003a, ///  /// USERPROFILE\Local Settings\Application Data\Microsoft\CD Burning ///  CSIDL_CDBURN_AREA = 0x003b, ///  /// Computers Near Me (computered from Workgroup membership) ///  CSIDL_COMPUTERSNEARME = 0x003d, ///  /// combine with CSIDL_ value to force folder creation in SHGetFolderPath() ///  CSIDL_FLAG_CREATE = 0x8000, ///  /// combine with CSIDL_ value to return an unverified folder path ///  CSIDL_FLAG_DONT_VERIFY = 0x4000, ///  /// combine with CSIDL_ value to insure non-alias versions of the pidl ///  CSIDL_FLAG_NO_ALIAS = 0x1000, ///  /// combine with CSIDL_ value to indicate per-user init (eg. upgrade) ///  CSIDL_FLAG_PER_USER_INIT = 0x0800, ///  /// mask for all possible ///  CSIDL_FLAG_MASK = 0xFF00, } public enum SHGetFolderLocationReturnValues : uint { ///  /// Success ///  S_OK = 0x00000000, ///  /// The CSIDL in nFolder is valid but the folder does not exist ///  S_FALSE = 0x00000001, ///  /// The CSIDL in nFolder is not valid ///  E_INVALIDARG = 0x80070057 } public static IntPtr GetPidlFromFolderID(IntPtr hWnd, CSIDL Id) { IntPtr pIdl = IntPtr.Zero; SHGetFolderLocationReturnValues res = (SHGetFolderLocationReturnValues) SHGetSpecialFolderLocation( hWnd, Id, out pIdl); return (pIdl); } public Form1() { InitializeComponent(); SampleProc = new WindProc (SubclassWndProc); OldDefProc = GetWindowLong(this.Handle, GWL_WNDPROC); SetWindowLong(this.Handle, GWL_WNDPROC, Marshal.GetFunctionPointerForDelegate(SampleProc)/*SampleProc.Method.MethodHandle.Value.ToInt32()*/); //tagSHCHANGENOTIFYENTRY changeentry = new tagSHCHANGENOTIFYENTRY(); //changeentry.dwEventMask = (ulong)SHCNE.SHCNE_ALLEVENTS; //changeentry.fRecursive = true; //changeentry.WatchDir = null; //SHChangeNotifyRegister(this.Handle, ref changeentry); SHChangeNotifyEntry changeentry = new SHChangeNotifyEntry(); changeentry.pIdl = GetPidlFromFolderID(this.Handle, CSIDL.CSIDL_DESKTOP); changeentry.Recursively = true; try { uint notifyid = SHChangeNotifyRegister( this.Handle, SHCNF.SHCNF_TYPE | SHCNF.SHCNF_IDLIST, SHCNE.SHCNE_ALLEVENTS, WM_FILECHANGEINFO, 1, ref changeentry); } catch (Exception ee) { } 

mi manca in SHChangeNotifyRegister per favore aiutami .. dimmi il motivo per cui sto andando in crash .. il codice dello stesso codice funziona bene per il desktop .. per favore aiuto

Grazie.

SHChangeNotifyRegister non è dichiarato correttamente, il codice sottostante ritorna correttamente, non sono sicuro di ricevere una notifica quando creo un file 🙂

  public partial class Form1 : Form { public Form1() { InitializeComponent(); SHChangeNotifyEntry changeEntry = new SHChangeNotifyEntry(); changeEntry.dwEventMask = (uint)SHCNE.SHCNE_CREATE; changeEntry.Recursive = false; changeEntry.pszWatchDir = "\\My Documents\\"; try { bool result = SHChangeNotifyRegister(this.Handle, ref changeEntry); } catch (Exception ee) { } } [DllImport("aygshell.dll")] private static extern bool SHChangeNotifyRegister(IntPtr hwnd, ref SHChangeNotifyEntry test); private void Form1_Load(object sender, EventArgs e) { } } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] struct SHChangeNotifyEntry { public uint dwEventMask; [MarshalAs(UnmanagedType.LPWStr)] public string pszWatchDir; public bool Recursive; } }