コード例 #1
0
 public override string ToString()
 {
     FieldInfo[] fields = typeof(HRESULT).GetFields(BindingFlags.Static | BindingFlags.Public);
     for (int i = 0; i < fields.Length; i++)
     {
         FieldInfo fieldInfo = fields[i];
         if (fieldInfo.FieldType == typeof(HRESULT))
         {
             HRESULT hrLeft = (HRESULT)fieldInfo.GetValue(null);
             if (hrLeft == this)
             {
                 string result = fieldInfo.Name;
                 return(result);
             }
         }
     }
     if (this.Facility == Facility.Win32)
     {
         FieldInfo[] fields2 = typeof(Win32Error).GetFields(BindingFlags.Static | BindingFlags.Public);
         for (int j = 0; j < fields2.Length; j++)
         {
             FieldInfo fieldInfo2 = fields2[j];
             if (fieldInfo2.FieldType == typeof(Win32Error))
             {
                 Win32Error error = (Win32Error)fieldInfo2.GetValue(null);
                 if ((HRESULT)error == this)
                 {
                     string result = "HRESULT_FROM_WIN32(" + fieldInfo2.Name + ")";
                     return(result);
                 }
             }
         }
     }
     return(string.Format(CultureInfo.InvariantCulture, "0x{0:X8}", new object[]
     {
         this._value
     }));
 }
コード例 #2
0
 static Win32Error()
 {
     Win32Error.ERROR_SUCCESS              = new Win32Error(0);
     Win32Error.ERROR_INVALID_FUNCTION     = new Win32Error(1);
     Win32Error.ERROR_FILE_NOT_FOUND       = new Win32Error(2);
     Win32Error.ERROR_PATH_NOT_FOUND       = new Win32Error(3);
     Win32Error.ERROR_TOO_MANY_OPEN_FILES  = new Win32Error(4);
     Win32Error.ERROR_ACCESS_DENIED        = new Win32Error(5);
     Win32Error.ERROR_INVALID_HANDLE       = new Win32Error(6);
     Win32Error.ERROR_OUTOFMEMORY          = new Win32Error(14);
     Win32Error.ERROR_NO_MORE_FILES        = new Win32Error(18);
     Win32Error.ERROR_SHARING_VIOLATION    = new Win32Error(32);
     Win32Error.ERROR_INVALID_PARAMETER    = new Win32Error(87);
     Win32Error.ERROR_INSUFFICIENT_BUFFER  = new Win32Error(122);
     Win32Error.ERROR_NESTING_NOT_ALLOWED  = new Win32Error(215);
     Win32Error.ERROR_KEY_DELETED          = new Win32Error(1018);
     Win32Error.ERROR_NOT_FOUND            = new Win32Error(1168);
     Win32Error.ERROR_NO_MATCH             = new Win32Error(1169);
     Win32Error.ERROR_BAD_DEVICE           = new Win32Error(1200);
     Win32Error.ERROR_CANCELLED            = new Win32Error(1223);
     Win32Error.ERROR_CLASS_ALREADY_EXISTS = new Win32Error(1410);
     Win32Error.ERROR_INVALID_DATATYPE     = new Win32Error(1804);
 }
コード例 #3
0
 public static void ThrowLastError()
 {
     ((HRESULT)Win32Error.GetLastError()).ThrowIfFailed();
 }
コード例 #4
0
ファイル: ErrorCodes.cs プロジェクト: yk2012985/wpf
 public static void ThrowLastError()
 {
     ((HRESULT)Win32Error.GetLastError()).ThrowIfFailed();
     // Only expecting to call this when we're expecting a failed GetLastError()
     Assert.Fail();
 }
コード例 #5
0
        public static IEnumerable <FileInfo> GetFiles(DirectoryInfo startDirectory, string pattern, bool recurse)
        {
            // We suppressed this demand for each p/invoke call, so demand it upfront once
            new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();

            // Validate parameters
            Verify.IsNotNull(startDirectory, "startDirectory");
            Verify.IsNeitherNullNorEmpty(pattern, "pattern");

            // Setup
            var findData    = new WIN32_FIND_DATAW();
            var directories = new Stack <DirectoryInfo>();

            directories.Push(startDirectory);

            // Process each directory.  Only push new directories if we're recursing.
            ErrorModes origErrorMode = NativeMethods.SetErrorMode(ErrorModes.FailCriticalErrors);

            try
            {
                while (directories.Count > 0)
                {
                    // Get the name of the next directory and the corresponding search pattern
                    DirectoryInfo dir     = directories.Pop();
                    string        dirPath = dir.FullName.Trim();
                    if (dirPath.Length == 0)
                    {
                        continue;
                    }
                    char lastChar = dirPath[dirPath.Length - 1];
                    if (lastChar != Path.DirectorySeparatorChar && lastChar != Path.AltDirectorySeparatorChar)
                    {
                        dirPath += Path.DirectorySeparatorChar;
                    }

                    // Process all files in that directory
                    using (SafeFindHandle handle = NativeMethods.FindFirstFileW(dirPath + pattern, findData))
                    {
                        Win32Error error;
                        if (handle.IsInvalid)
                        {
                            error = Win32Error.GetLastError();
                            if (error == Win32Error.ERROR_ACCESS_DENIED || error == Win32Error.ERROR_FILE_NOT_FOUND)
                            {
                                continue;
                            }
                            Assert.AreNotEqual(Win32Error.ERROR_SUCCESS, error);
                            ((HRESULT)error).ThrowIfFailed();
                        }

                        do
                        {
                            if (!Utility.IsFlagSet((int)findData.dwFileAttributes, (int)FileAttributes.Directory))
                            {
                                yield return(new FileInfo(dirPath + findData.cFileName));
                            }
                        }while (NativeMethods.FindNextFileW(handle, findData));
                        error = Win32Error.GetLastError();
                        if (error != Win32Error.ERROR_NO_MORE_FILES)
                        {
                            ((HRESULT)error).ThrowIfFailed();
                        }
                    }

                    // Push subdirectories onto the stack if we are recursing.
                    if (recurse)
                    {
                        // In a volatile system we can't count on all the file information staying valid.
                        // Catch reasonable exceptions and move on.
                        try
                        {
                            foreach (DirectoryInfo childDir in dir.GetDirectories())
                            {
                                try
                                {
                                    FileAttributes attrib = File.GetAttributes(childDir.FullName);
                                    // If it's not a hidden, system folder, nor a reparse point
                                    if (!Utility.IsFlagSet((int)attrib, (int)(FileAttributes.Hidden | FileAttributes.System | FileAttributes.ReparsePoint)))
                                    {
                                        directories.Push(childDir);
                                    }
                                }
                                catch (FileNotFoundException)
                                {
                                    // Shouldn't see this.
                                    Assert.Fail();
                                }
                                catch (DirectoryNotFoundException) { }
                            }
                        }
                        catch (DirectoryNotFoundException) { }
                    }
                }
            }
            finally
            {
                NativeMethods.SetErrorMode(origErrorMode);
            }
        }