/// <summary> /// Reads WSL-related information from a registry key and returns it as a model object. /// </summary> /// <param name="lxssKey">Registry key from which to read information.</param> /// <param name="keyName">The GUID name under the LXSS registry key.</param> /// <param name="parsedDefaultGuid">Default distribution's GUID key as recorded in the LXSS registry key.</param> /// <returns>Returns the WSL distribution information obtained through registry information.</returns> private static DistroRegistryInfo ReadFromRegistryKey(RegistryKey lxssKey, string keyName, Guid?parsedDefaultGuid) { if (!Guid.TryParse(keyName, out Guid parsedGuid)) { return(null); } using (var distroKey = lxssKey.OpenSubKey(keyName)) { var distroName = distroKey.GetValue("DistributionName", default(string)) as string; if (string.IsNullOrWhiteSpace(distroName)) { return(null); } var basePath = distroKey.GetValue("BasePath", default(string)) as string; var normalizedPath = Path.GetFullPath(basePath); var kernelCommandLine = (distroKey.GetValue("KernelCommandLine", default(string)) as string ?? string.Empty); var result = new DistroRegistryInfo() { DistroId = parsedGuid, DistroName = distroName, BasePath = normalizedPath, }; result.KernelCommandLine.AddRange(kernelCommandLine.Split( new char[] { ' ', '\t', }, StringSplitOptions.RemoveEmptyEntries)); if (parsedDefaultGuid.HasValue && parsedDefaultGuid == parsedGuid) { result.IsDefault = true; return(result); } } return(null); }
/// <summary> /// Execute the specified command through the default shell of a specific WSL distribution, and get the result as a System.IO.Stream object. /// </summary> /// <param name="distroInfo">The WSL distribution on which to run the command.</param> /// <param name="commandLine">The command you want to run.</param> /// <param name="outputStream">The System.IO.Stream object to receive the results. It must be writable.</param> /// <param name="bufferLength">Specifies the size of the buffer array to use when copying from anonymous pipes to the underlying stream. You do not need to specify a value.</param> /// <returns>Returns the sum of the number of bytes received.</returns> public static long RunWslCommand(this DistroRegistryInfo distroInfo, string commandLine, Stream outputStream, int bufferLength = 65535) { return(Wsl.RunWslCommand(distroInfo.DistroName, commandLine, outputStream, bufferLength)); }