Ejemplo n.º 1
0
        private static Exception ThrowLastWin32Error(string path, string message, int?lastErrorArg = null)
        {
            var lastError = lastErrorArg ?? Marshal.GetLastWin32Error();

            if (OperatingSystemHelper.IsUnixOS)
            {
                throw new IOException(message);
            }
            else
            {
                var errorMessage = NativeMethods.GetErrorMessage(lastError);
                message = string.Format(CultureInfo.InvariantCulture, "{0} last error: [{1}] (error code {2})", message, errorMessage, lastError);

                switch (lastError)
                {
                case ERROR_FILE_NOT_FOUND:
                    throw new FileNotFoundException(message);

                case ERROR_PATH_NOT_FOUND:
                    throw new DirectoryNotFoundException(message);

                case ERROR_ACCESS_DENIED:
                case ERROR_SHARING_VIOLATION:

                    string extraMessage = string.Empty;

                    if (path != null)
                    {
                        extraMessage = " " + (FileUtilities.TryFindOpenHandlesToFile(path, out var info, printCurrentFilePath: false)
                                ? info
                                : "Attempt to find processes with open handles to the file failed.");
                    }

                    throw new UnauthorizedAccessException($"{message}.{extraMessage}");

                default:
                    throw new IOException(message, ExceptionUtilities.HResultFromWin32(lastError));
                }
            }
        }
 private static bool LoadFilterSuccessful(int win32Error)
 {
     // The result of trying to load the filter should be either S_OK or the filter should be there/already running
     return(win32Error == 0 || win32Error == ExceptionUtilities.HResultFromWin32(ERROR_ALREADY_EXISTS) || win32Error == ExceptionUtilities.HResultFromWin32(ERROR_SERVICE_ALREADY_RUNNING));
 }