using System; using System.Runtime.InteropServices; using Vanara.InteropServices; using Vanara.PInvoke; using static Vanara.PInvoke.BITS; namespace Vanara.IO { /// Exceptions specific to BITS public class BackgroundCopyException : Exception { private readonly HRESULT code; private readonly BG_ERROR_CONTEXT ctx; private readonly string ctxDesc, errDesc, protocol; private readonly IBackgroundCopyFile iVal; internal BackgroundCopyException(IBackgroundCopyError err) { const uint lang = 0x1; // LANG_NEUTRAL, SUBLANG_DEFAULT err.GetError(out ctx, out code); try { ctxDesc = err.GetErrorContextDescription(lang); } catch { ctxDesc = SafeCoTaskMemString.Null; } try { errDesc = err.GetErrorDescription(lang); } catch { errDesc = SafeCoTaskMemString.Null; } try { protocol = err.GetProtocol(); } catch { protocol = SafeCoTaskMemString.Null; } iVal = err.GetFile(); } internal BackgroundCopyException(COMException cex) { code = cex.ErrorCode; errDesc = BackgroundCopyManager.GetErrorMessage(code); if (errDesc is null) code.ThrowIfFailed(); } /// Context in which the error occurred. public BackgroundCopyErrorContext Context => (BackgroundCopyErrorContext)ctx; /// Description of the context in which the error occurred. public string ContextDescription => ctxDesc; /// Gets the error code. /// The error code. public HRESULT ErrorCode => code; /// If error was related to a file, returns information about the file and its progress. Otherwise, returns NULL. public BackgroundCopyFileInfo File { get { if (iVal is null) return null; return new BackgroundCopyFileInfo(iVal); } } /// The error text associated with the error. public override string Message => errDesc; /// /// Contains the protocol used to transfer the file. The string contains "http" for the HTTP protocol and "file" for the SMB protocol and to NULL if the /// error is not related to the transfer protocol. /// public string Protocol => protocol; } }