zoukankan      html  css  js  c++  java
  • dotnet 执行命令常用代码

    private int Execute(string str, bool silence = false)
    {
        int result;
        try
        {
            Console.WriteLine("nuget " + str);
            Process process = this.ExecuteCMD(str);
            StringBuilder stringBuilder = new StringBuilder();
            using (StreamReader standardOutput = process.StandardOutput)
            {
                while (!standardOutput.EndOfStream)
                {
                    string text = standardOutput.ReadLine();
                    Console.WriteLine(text);
                    if (text.Contains("forbidden"))
                    {
                        process.Kill();
                        process.WaitForExit();
                        throw new Exception("权限不足");
                    }
                    stringBuilder.Append(text);
                }
            }
            string str2 = process.StandardError.ReadToEnd();
            process.WaitForExit();
            int exitCode= process.ExitCode;
            if (exitCode!= 0) { 

            string text2 = stringBuilder.ToString() + " " + str2; Console.WriteLine(text2);
            
    if (!silence) {
              
    throw new Exception(text2);
            }
          Console.WriteLine(
    "nuget operation failed to achieve the desired effect.");
      }
       result
    =exitCode;
    }
    catch (Exception) { throw; }
    return result;
    }
    private Process ExecuteCMD(string str)
    {
        ProcessStartInfo startInfo;
        if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
        {
            str = " /usr/local/bin/nuget.exe " + str;
            startInfo = new ProcessStartInfo("mono", str)
            {
                RedirectStandardOutput = true,
                CreateNoWindow = false,
                ErrorDialog = false,
                RedirectStandardError = true
            };
        }
        else
        {
            startInfo = new ProcessStartInfo("nuget", str)
            {
                RedirectStandardOutput = true,
                CreateNoWindow = false,
                ErrorDialog = false,
                RedirectStandardError = true
            };
        }
        return Process.Start(startInfo);
    }
    private string ExecuteCMDWithOutput(string str)
    {
        Process process = Process.Start(new ProcessStartInfo("nuget", str)
        {
            RedirectStandardOutput = true,
            CreateNoWindow = false
        });
        string result = string.Empty;
        using (StreamReader standardOutput = process.StandardOutput)
        {
            while (!standardOutput.EndOfStream)
            {
                string text = standardOutput.ReadLine();
                if (text.Contains("xxx") || text.Contains("xxx"))
                {
                    result = text;
                }
            }
            if (!process.HasExited)
            {
                process.Kill();
                return result;
            }
        }
        return result;
    }
  • 相关阅读:
    HDU 1560 DNA sequence (迭代加深搜索)
    POJ-1077 HDU 1043 HDU 3567 Eight (BFS预处理+康拓展开)
    CSUST 1011 神秘群岛 (Dijkstra+LCA)
    LCA 倍增
    HDU 1003 Max Sum 求区间最大值 (尺取法)
    Codeforce 867 C. Ordering Pizza (思维题)
    POJ 3349 Snowflake Snow Snowflakes (Hash)
    POJ 2774 Long Long Message (Hash + 二分)
    POJ 1200 Crazy Search (Hash)
    前端面试总结(转)
  • 原文地址:https://www.cnblogs.com/wolbo/p/12196439.html
Copyright © 2011-2022 走看看