C# problem with calling command using Process.Run()

I’ve encountered strange error with calling command from C# code using Process.Run().

I’ve some app and this app uses similar way of executing other commands without any issues.
Now I’m writing UnitTests using MSTest library and got strange problems with the same task.

I’ve copied this to new Console project to make sure it’s not something connected with Tests project only.

Working code in my app (main parts):

Factory:

private Process CreatePowershellProcess(string args) {
    _systemInfo.Validate();
    if(!args.StartsWith("-Command")) {
        args = "-Command " + args;
    }
    return new Process {
        StartInfo = new ProcessStartInfo {
            FileName = _systemInfo.GetShellInterpreterName(),
            Arguments = args,
            UseShellExecute = false,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            CreateNoWindow = true
        },
        EnableRaisingEvents = true
    };
}

Usage:

public string? ExecCommand(string command) {
    var proc = _factory.CreateShellProcess(command);
    proc.Start();
    string output = proc.StandardOutput.ReadToEnd();
    proc.WaitForExit();
    return output;
}

Note: ProcessFactory.CreateShellProcess(string command) is an umbrella function that on Windows (by default or on Linux if config points app should use powershell) redirects to CreatePowershellProcess().

For development we use only Windows machines, but in Unit Tests we also want to test executing commands on Linux. So I make up using WSL to do so.

At the begining I’m trying to check whether there is WSL enabled:

private bool IsWSLInstalled() {
    return RunWslProcess("--help");
}

private bool RunWslProcess(string args) {
    try {
        var proc = _processFactory.CreateShellProcess("wsl.exe " + args);
        proc.Start();
        string output = proc.StandardOutput.ReadToEnd();
        string error = proc.StandardError.ReadToEnd();
        proc.WaitForExit();
        return proc.ExitCode == 0;
    } catch(Exception exc) {
        Debug.WriteLine(exc.ToString());
        return false;
    }
}

I’ve tried many modifications of this code and it freezes the app or do what I will describe below.

To debug it I’ve created over-simplified code in new Console app project:

internal class Program {
    public static void Main(string[] args) {
        Console.WriteLine("Started...");
        bool status = CheckWslAvaliable();
        Console.WriteLine("WSL Status: " + status);
    }

    public static bool CheckWslAvaliable() {
        var process = new Process {
            StartInfo = new ProcessStartInfo {
                FileName = "powershell",
                Arguments = "-Command wsl --help",
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true
            }
        };
        process.Start();
        var output = process.StandardOutput.ReadToEnd();
        var error = process.StandardError.ReadToEnd();
        process.WaitForExit();
        Debug.WriteLine(output);
        Debug.WriteLine(error);
        Debug.WriteLine(process.ExitCode);
        return process.ExitCode == 0;
    }
}

The behavior of this piece of code is exactly the same as the one in unit tests.

  1. the process always has ExitCode = 1
  2. standard error is empty which suggest the command execution went well
  3. standard output has strange encoding like:
    "Copyright (c) Microsoft Corporation.[...]"
    all letters are separated by nulls and to debug only C is being print. I’ve not experienced anything like that in other places calling Process.Run().
  4. If I cut this “” in notepad I got desired result – normal wsl –help output.
  5. It’s producing the same result if I just set FileName = "wsl" and Arguments=”–help”
  6. For non existing wsl switch I also got ExitCode = 1 and empty error, but output contains appropriate error message (with same wrong encoding).
  7. If I call powershell -C wsl --help or wsl --help from: powrshell window, cmd window or Ctrl+R manu, It works fine.

Why am I always getting status code 1?
Why output here is so messy?
Why error is always empty?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật