⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filesystemwatcher.cs

📁 蓝牙通讯
💻 CS
📖 第 1 页 / 共 2 页
字号:
//			{
//				return synchronizingObject;
//
//			}
//		}


		internal void Remove()
		{
#if !NDOC
			SHChangeNotifyDeregister(windowSink.Hwnd);
#endif
		}

		internal void AddDir()
		{	
			SHCHANGENOTIFYENTRY notEntry = new SHCHANGENOTIFYENTRY();

			//Set mask
			notEntry.dwEventMask = (int)SHCNE_ALLEVENTS;
			//notEntry.dwEventMask = (int)SHCNE_ATTRIBUTES | (int)SHCNE_UPDATEDIR | (int)SHCNE_UPDATEITEM;
			//notEntry.dwEventMask = (int)SHCNE_UPDATEDIR | (int)SHCNE_UPDATEITEM;
			
			notEntry.fRecursive = BOOL(includeSubdirectories);

			//Set watch dir
			IntPtr lpWatchDir = StringToHLocal(path);
			notEntry.pszWatchDir = lpWatchDir;
			
			//Call API
#if !NDOC
			int res = SHChangeNotifyRegister(windowSink.Hwnd, ref notEntry);
#endif
							
		}
       
		//Destructor
		/// <summary>
		/// Releases unmanaged resources and performs other cleanup operations before the <see cref="FileSystemWatcher"/> is reclaimed by garbage collection.
		/// </summary>
		~FileSystemWatcher()
		{
			this.Dispose(true);
		}

		#region virtual events

		
		private void OnInternalFileSystemEventArgs(object sender, FileSystemEventArgs e) 
		{
			lock (this)
				//if (!(this.isChanged)) 
				{
					//this.changedResult = new WaitForChangedResult(e.ChangeType, e.Name, false);
					//this.isChanged = 1;
					//Monitor.Pulse(this);
				}
		}


		public virtual void OnChanged(FileSystemEventArgs e)
		{
			lock (this)
			{	
				if (this.Changed!=null)
				{
					this.Changed(this, e);
				}
			}
		}

		public virtual void OnCreated(FileSystemEventArgs e)
		{
			lock (this)
			{
				if (this.Created!=null)
					this.Created(this, e);
			}
		}

		public virtual void OnDeleted(FileSystemEventArgs e)
		{
			lock (this)
			{
				if (this.Deleted!=null)
					this.Deleted(this, e);
			}
		}

		public virtual void OnRenamed(RenamedEventArgs e)
		{
			lock (this)
			{
				if (this.Renamed!=null)
					this.Renamed(this, e);
			}
		}


		#endregion

		#region API Declarations

		private const uint LMEM_FIXED = 0;
		private const uint LMEM_ZEROINIT = 0x0040;
		private const uint LPTR = (LMEM_FIXED | LMEM_ZEROINIT);


		internal static int BOOL(bool value)
		{
			if (value)
				return 1;
			else
				return 0;
		}

		[DllImport("coredll.dll")]
		internal static extern IntPtr LocalAlloc(
			uint uFlags, 
			uint uBytes ); 
		
		[DllImport("coredll.dll")]
		internal static extern IntPtr LocalFree(
			IntPtr hMem ); 


		internal static IntPtr AllocHGlobal(int cb) 
		{
			IntPtr hMem;

			hMem = LocalAlloc(0, (uint)cb);
			if (hMem == IntPtr.Zero)
				throw new OutOfMemoryException();
			return hMem;
		}

		private static IntPtr AllocHLocal( int bytes )
		{
			return( LocalAlloc(LPTR, (uint)bytes) );
		}


		internal static IntPtr StringToHLocal( string s )
		{
			if( s == null ) 
			{
				return( IntPtr.Zero );
			} 
			else 
			{
				int nc = s.Length;
				int len = 2*(1+nc);
				IntPtr hLocal = AllocHLocal( len );
				if( hLocal == IntPtr.Zero ) 
				{
					throw new OutOfMemoryException();
				} 
				else 
				{
					Marshal.Copy( s.ToCharArray(), 0, hLocal, s.Length );
					return( hLocal );
				}
			}
		}


		internal static void FreeHGlobal(IntPtr hglobal) 
		{
			LocalFree(hglobal);
		}
		[DllImport("aygshell.dll")]
		internal static extern int SHChangeNotifyRegister (
			IntPtr  hwnd,
			ref SHCHANGENOTIFYENTRY pshcne );

		[DllImport("aygshell.dll")]
		internal static extern int SHChangeNotifyRegister (
			IntPtr  hwnd,
			IntPtr pshcne );


		[DllImport("aygshell.dll")]
		internal static extern void SHChangeNotifyFree(IntPtr pshcne);

		[DllImport("aygshell.dll")]
		internal static extern int SHChangeNotifyDeregister(
			IntPtr  hwnd);


		internal struct SHCHANGENOTIFYENTRY 
		{
			internal int  dwEventMask;
			internal IntPtr  pszWatchDir;
			internal int  fRecursive;
		} 

		
		internal const long SHCNE_RENAME	        =  0x00000001L;   // GOING AWAY
		internal const long SHCNE_RENAMEITEM     =     0x00000001L;
		internal const long SHCNE_CREATE	        =  0x00000002L;
		internal const long SHCNE_DELETE	        =  0x00000004L;
		internal const long SHCNE_MKDIR	        =      0x00000008L;
		internal const long SHCNE_RMDIR          =     0x00000010L;
		internal const long SHCNE_MEDIAINSERTED  =     0x00000020L;
		internal const long SHCNE_MEDIAREMOVED   =     0x00000040L;
		internal const long SHCNE_DRIVEREMOVED   =     0x00000080L;
		internal const long SHCNE_DRIVEADD       =     0x00000100L;
		internal const long SHCNE_NETSHARE       =     0x00000200L;
		internal const long SHCNE_NETUNSHARE     =     0x00000400L;
		internal const long SHCNE_ATTRIBUTES     =     0x00000800L;
		internal const long SHCNE_UPDATEDIR      =     0x00001000L;
		internal const long SHCNE_UPDATEITEM     =     0x00002000L;
		internal const long SHCNE_SERVERDISCONNECT =   0x00004000L;
		internal const long SHCNE_UPDATEIMAGE      =   0x00008000L;
		internal const long SHCNE_DRIVEADDGUI      =   0x00010000L;
		internal const long SHCNE_RENAMEFOLDER     =   0x00020000L;
		internal const long SHCNE_ALLEVENTS       =    0x7FFFFFFFL;

		internal const int WM_FILECHANGEINFO  = (0x8000 + 0x101);


		internal struct FILECHANGENOTIFY 
		{
			internal int  dwRefCount;
			internal int cbSize;
			internal int wEventId;
			internal uint uFlags;             
			internal int dwItem1;            
			internal int dwItem2;
			internal int dwAttributes;  
			internal int dwLowDateTime; 
			internal int dwHighDateTime;   
			internal uint nFileSize;      
		} 


		#endregion


		#region IDisposable Members

		private bool disposed = false;

		/// <summary>
		/// Releases the resources used by the <see cref="FileSystemWatcher"/>.
		/// </summary>
		public void Dispose()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}

		/// <summary>
		/// Releases the unmanaged resources used by the <see cref="FileSystemWatcher"/> and optionally releases the managed resources.
		/// </summary>
		/// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
		protected override void Dispose(bool disposing)
		{
			// Check to see if Dispose has already been called.
			if(!this.disposed)
			{
				// Release unmanaged resources. If disposing is false, 
				// only the following code is executed.
				if (enableRaisingEvents)
					this.Remove();

			}
			disposed = true;         
		}

		#endregion

		#region WindowSink
#if !NDOC
		internal class WindowSink : Microsoft.WindowsCE.Forms.MessageWindow
		{

			private FileSystemWatcher watcher;

			public WindowSink(FileSystemWatcher w)
			{
				watcher = w;	
			}
			
			//helper function
			private bool ValidateByFilter(string fileName)
			{
				
				if (watcher.filter != "*.*")
				{
					string filterName = System.IO.Path.GetFileNameWithoutExtension(watcher.filter);
					string filterExt = System.IO.Path.GetExtension(watcher.filter);
						
					if (filterName == "*") //check ext only
					{
						if (System.IO.Path.GetExtension(fileName).ToLower() != filterExt.ToLower())
						{
							return false;
						}
						else 
							return true;
					}
					else //we've got name in the filter
					{
						if (filterExt != ".*") //star in the ext
						{
							if (System.IO.Path.GetFileNameWithoutExtension(fileName).ToLower() != filterName.ToLower())
							{
								return  false;
							}
							else
								return true;
						}
						else //name and ext supplied
						{
							if (System.IO.Path.GetFileNameWithoutExtension(fileName).ToLower() != filterName.ToLower())
							{
								return  false;
							}
							else
								return true;

						}
					}

				}
				return true;

			}


			protected override void WndProc(ref Microsoft.WindowsCE.Forms.Message msg)
			{
							 
				if (msg.Msg == WM_FILECHANGEINFO)
				{
					if (msg.LParam == IntPtr.Zero)
						return;
					
					if ((int)msg.LParam == 0)
						return;				
	

					FILECHANGENOTIFY fchnot = (FILECHANGENOTIFY)Marshal.PtrToStructure(msg.LParam, typeof(FILECHANGENOTIFY));
								
					string fullPath = Marshal.PtrToStringUni((IntPtr)fchnot.dwItem1);
					string oldfullPath = Marshal.PtrToStringUni((IntPtr)fchnot.dwItem2);
					string fileName = System.IO.Path.GetFileName(fullPath);
					string dirName = System.IO.Path.GetDirectoryName(fullPath);
					string oldfileName = "";
			
			
					if (ValidateByFilter(fileName))
					{
						FileSystemEventArgs args;
						RenamedEventArgs renArgs;
								
						switch (fchnot.wEventId)
						{
							case (int)SHCNE_CREATE:
								if (watcher.notifyFilter == NotifyFilters.FileName)
								{
									args = new FileSystemEventArgs(WatcherChangeTypes.Created, dirName, fileName);
									watcher.OnCreated(args);
								}
								break;
							case (int)SHCNE_MKDIR:
								if ((watcher.notifyFilter & NotifyFilters.DirectoryName) == NotifyFilters.DirectoryName)
								{
									args = new FileSystemEventArgs(WatcherChangeTypes.Created, dirName, fileName);
									watcher.OnCreated(args);
								}
								break;
							case (int)SHCNE_UPDATEDIR:		
								if ((watcher.notifyFilter & NotifyFilters.DirectoryName) == NotifyFilters.DirectoryName)
								{
									args = new FileSystemEventArgs(WatcherChangeTypes.Changed, dirName, fileName);
									watcher.OnChanged(args);
								}
								break;		
						   case (int)SHCNE_RMDIR:		
								if ((watcher.notifyFilter & NotifyFilters.DirectoryName) == NotifyFilters.DirectoryName)
								{
									args = new FileSystemEventArgs(WatcherChangeTypes.Deleted, dirName, fileName);
									watcher.OnChanged(args);
								}
								break;
							case (int)SHCNE_DELETE:
								if ((watcher.notifyFilter & NotifyFilters.FileName) == NotifyFilters.FileName)
								{
									args = new FileSystemEventArgs(WatcherChangeTypes.Deleted, dirName, fileName);
									watcher.OnDeleted(args);
								}
								break;
							case (int)SHCNE_UPDATEITEM:
								if ((watcher.notifyFilter & NotifyFilters.FileName) == NotifyFilters.FileName)
								{
									args = new FileSystemEventArgs(WatcherChangeTypes.Changed, dirName, fileName);
									watcher.OnChanged(args);
								}
								break;
							case (int)SHCNE_RENAMEFOLDER:
								if ((watcher.notifyFilter & NotifyFilters.DirectoryName) == NotifyFilters.DirectoryName)
								{
									 oldfileName = Marshal.PtrToStringUni((IntPtr)fchnot.dwItem2);
									renArgs = new RenamedEventArgs(WatcherChangeTypes.Renamed, dirName, fileName, oldfileName);
									watcher.OnRenamed(renArgs);
								}
								break;
							case (int)SHCNE_RENAME:	
								if ((watcher.notifyFilter & NotifyFilters.FileName) == NotifyFilters.FileName)
								{
									oldfileName = Marshal.PtrToStringUni((IntPtr)fchnot.dwItem2);
									renArgs = new RenamedEventArgs(WatcherChangeTypes.Renamed, dirName, fileName, oldfileName);
									watcher.OnRenamed(renArgs);
								}
								break;
			
							case (int)SHCNE_ATTRIBUTES:
								if ((watcher.notifyFilter & NotifyFilters.Attributes) == NotifyFilters.Attributes)
								{
									args = new FileSystemEventArgs(WatcherChangeTypes.Changed, dirName, fileName);
									watcher.OnChanged(args);
								}
								break;
			
						}
					}
			
					SHChangeNotifyFree(msg.LParam);
				}
			
				msg.Result = (IntPtr)0;
				base.WndProc(ref msg);					
			}

		}
#endif


		#endregion
	}

	#endregion

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -