コード例 #1
0
        private void StartRaisingEvents()
        {
            //Cannot allocate the directoryHandle and the readBuffer if the object has been disposed; finalization has been suppressed.
            if (this.disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            try {
                new EnvironmentPermission(PermissionState.Unrestricted).Assert();
                if (Environment.OSVersion.Platform != PlatformID.Win32NT)
                {
                    throw new PlatformNotSupportedException(SR.GetString(SR.WinNTRequired));
                }
            }
            finally {
                CodeAccessPermission.RevertAssert();
            }

            // If we're called when "Initializing" is true, set enabled to true
            if (IsSuspended())
            {
                enabled = true;
                return;
            }

            if (!readGranted)
            {
                string fullPath;
                // Consider asserting path discovery permission here.
                fullPath = System.IO.Path.GetFullPath(directory);

                FileIOPermission permission = new FileIOPermission(FileIOPermissionAccess.Read, fullPath);
                permission.Demand();
                readGranted = true;
            }


            // If we're attached, don't do anything.
            if (!IsHandleInvalid)
            {
                return;
            }

            // Create handle to directory being monitored
            directoryHandle = NativeMethods.CreateFile(directory,                               // Directory name
                                                       UnsafeNativeMethods.FILE_LIST_DIRECTORY, // access (read-write) mode
                                                       UnsafeNativeMethods.FILE_SHARE_READ |
                                                       UnsafeNativeMethods.FILE_SHARE_DELETE |
                                                       UnsafeNativeMethods.FILE_SHARE_WRITE,     // share mode
                                                       null,                                     // security descriptor
                                                       UnsafeNativeMethods.OPEN_EXISTING,        // how to create
                                                       UnsafeNativeMethods.FILE_FLAG_BACKUP_SEMANTICS |
                                                       UnsafeNativeMethods.FILE_FLAG_OVERLAPPED, // file attributes
                                                       new SafeFileHandle(IntPtr.Zero, false)    // file with attributes to copy
                                                       );

            if (IsHandleInvalid)
            {
                throw new FileNotFoundException(SR.GetString(SR.FSW_IOError, directory));
            }

            stopListening = false;
            // Start ignoring all events that were initiated before this.
            Interlocked.Increment(ref currentSession);

            // Attach handle to thread pool

            //SECREVIEW: At this point at least FileIOPermission has already been demanded.
            SecurityPermission secPermission = new SecurityPermission(PermissionState.Unrestricted);

            secPermission.Assert();
            try {
                ThreadPool.BindHandle(directoryHandle);
            }
            finally {
                SecurityPermission.RevertAssert();
            }
            enabled = true;

            // Setup IO completion port
            Monitor(null);
        }