YaKun 的头像
YaKun

技术笔记与日常记录

179 字
1 分钟

.NET通过CMD执行命令并获取输出【音乐小工具其一】

2023-01-12
无标签

最近研究VIP音乐解密,发现作者提供了一个CLI,于是准备写个监听小程序,监听下载音乐文件夹,自动通过CLI解密然后上传到我的NAS,这个CLI比较特殊,输出信息是通过Error获取到的,拿Output反而会卡住,比较不寻常,记录一下执行方法。

/// <summary>
/// 执行命令行
/// </summary>
public static async Task<string> ExecuteCommandLine(this string shell, bool isAppendExit = true)
{
var process = new Process
{
StartInfo =
{
FileName = "cmd.exe",
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
StandardErrorEncoding = Encoding.Default
}
};
if (isAppendExit)
{
shell += "&exit";
}
process.Start();
await process.StandardInput.WriteLineAsync(shell);
var output = await process.StandardOutput.ReadToEndAsync();//输出信息
var error = await process.StandardError.ReadToEndAsync();//错误信息
await process.WaitForExitAsync();
process.Close();
return error;
}