最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I run a CMD from C#? - Stack Overflow

programmeradmin2浏览0评论

I am trying to manipulate my swagger doc. I need to run redoc split to separate the main swagger.

During start.cs I am getting the swagger doc and saving it to the file system. That works as expected. Then in my split method, I get the swagger doc and run it through redoc split and nothing happens, no error and no messages. I put breakpoints on the first line, inside the Try block and when I step, it exits so I think an error is getting swallowed somewhere.

I can run this from a command window just fine.

public static void RunSplitCommand()
{
    string output = string.Empty;
    string error = string.Empty;
    var args = "redocly split \"../bin/swagger/Swagger.json\"  --outDir= \"../bin/swagger\"";
    
    using (var process = new System.Diagnostics.Process())
    {
        process.StartInfo = new System.Diagnostics.ProcessStartInfo();
        process.StartInfo.WorkingDirectory = Directory.GetCurrentDirectory();
        process.StartInfo.FileName = "cmd.exe";
        process.StartInfo.Arguments = args;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = true;
        
        try
        {
            process.Start();
        
            process.WaitForExit();
            if (process.StandardOutput.ReadToEnd() != string.Empty)
                output = process.StandardOutput.ReadToEnd();
    
            else if (process.StandardError.ReadToEnd() != string.Empty)
                output = process.StandardError.ReadToEnd();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }
    
    string.Concat(output, "\n");
}
发布评论

评论列表(0)

  1. 暂无评论