// ReSharper disable once TooManyArguments public static extern int CheckElevation( string applicationName, ref uint flags, IntPtr childToken, out RunLevel runLevel, out RunLevelConclusionReason reason );
/// <summary> /// Checks a file and retrieve the expected <see cref="RunLevel" /> for it to start /// </summary> /// <param name="applicationAddress">Address of the file or the executable</param> /// <param name="reason">A value showing the reason of this conclusion</param> /// <returns>A value indicating the expected run level</returns> /// <exception cref="NotSupportedException">This method is only supported on Windows Vista+</exception> public static RunLevel GetExpectedRunlevel(string applicationAddress, out RunLevelConclusionReason reason) { try { RunLevel runLevel; uint pdwFlags = 0; var errorCode = Kernel.CheckElevation(applicationAddress, ref pdwFlags, IntPtr.Zero, out runLevel, out reason); if (errorCode == 0) // ERROR_SUCCESS { return(runLevel); } var exception = new Win32Exception(errorCode); switch (errorCode) { case 2: // ERROR_FILE_NOT_FOUND throw new FileNotFoundException(exception.Message, applicationAddress, exception); case 3: // ERROR_PATH_NOT_FOUND throw new DirectoryNotFoundException(exception.Message, exception); case 5: // ERROR_ACCESS_DENIED throw new UnauthorizedAccessException(exception.Message, exception); case 8: // ERROR_NOT_ENOUGH_MEMORY case 14: // ERROR_OUTOFMEMORY throw new InsufficientMemoryException(exception.Message, exception); case 15: // ERROR_INVALID_DRIVE throw new DriveNotFoundException(exception.Message, exception); default: throw exception; } } catch (EntryPointNotFoundException) { throw new NotSupportedException(); } }