/// <summary> /// Generate a DOS filename from a full filename. /// </summary> /// <param name="filename">The full filename.</param> /// <param name="allow_extended">True to allow extended characters.</param> /// <param name="iterations">Number of iterations of the algorithm to test.</param> /// <param name="throw_on_error">True throw on error.</param> /// <returns>The DOS filename.</returns> public static NtResult <string> Generate8dot3Name(string filename, bool allow_extended, int iterations, bool throw_on_error) { if (iterations <= 0) { throw new ArgumentException("Invalid iteration count."); } GenerateNameContext context = new GenerateNameContext() { NameBuffer = new byte[16], ExtensionBuffer = new byte[8] }; if (IsLegal8dot3Name(filename)) { return(filename.ToUpper().CreateResult()); } NtResult <string> result = default; for (int i = 0; i < iterations; ++i) { using (var name = new UnicodeStringAllocated(24)) { result = NtRtl.RtlGenerate8dot3Name(new UnicodeString(filename), allow_extended, ref context, name).CreateResult(throw_on_error, () => name.ToString()); } } return(result); }
/// <summary> /// Run a function on an NtResult and dispose the result afterwards. /// </summary> /// <typeparam name="T">The underlying result type.</typeparam> /// <typeparam name="S">The result of the function.</typeparam> /// <param name="result">The result.</param> /// <param name="func">The function to call.</param> /// <param name="default_value">The default value to return if an error occurred.</param> /// <returns>The result of func.</returns> /// <remarks>If result is not a success then the function is not called.</remarks> public static S RunAndDispose <T, S>(this NtResult <T> result, Func <T, S> func, S default_value) where T : NtObject { using (result) { if (!result.IsSuccess) { return(default_value); } return(func(result.Result)); } }
/// <summary> /// Run an action on an NtResult and dispose the result afterwards. /// </summary> /// <typeparam name="T">The underlying result type.</typeparam> /// <param name="result">The result.</param> /// <param name="action">The action to call.</param> /// <remarks>If result is not a success then the action is not called.</remarks> public static void RunAndDispose <T>(this NtResult <T> result, Action <T> action) where T : NtObject { if (!result.IsSuccess) { return; } using (result) { action(result.Result); } }
/// <summary> /// Run a function on an NtResult and dispose the result afterwards. /// </summary> /// <typeparam name="T">The underlying result type.</typeparam> /// <typeparam name="S">The result of the function.</typeparam> /// <param name="result">The result.</param> /// <param name="func">The function to call.</param> /// <returns>The result of func.</returns> /// <remarks>If result is not a success then the function is not called.</remarks> public static S RunAndDispose <T, S>(this NtResult <T> result, Func <T, S> func) where T : NtObject { return(RunAndDispose(result, func, default(S))); }