繁体   English   中英

C#中的命令行输出验证

[英]command line output validation in c#

我在实际命令提示符下的输出如下所示:

Name:   My Software
Version:  1.0.1
Installed location: c:\my folder

我正在尝试通过C#代码获取此输出

System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + "my command to execute");   

// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;

// Do not create the black window.
procStartInfo.CreateNoWindow = true;

// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();

// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();
string[] lines = result.Split(new string[] { System.Environment.NewLine, }, System.StringSplitOptions.None);
foreach (string tmp in lines)
{
    if (tmp.Contains("Version"))
    {
        isAvailable= true; 
    }
}

我不想只检查一个版本标记,我试图获取版本值并进行比较,例如,如果该值为1.0.1,则我想要该值并与2.0.0进行比较。

我可以使用indexof (例如result.IndexOf("Version:"); )-但这不能使我获得版本的价值

任何想法都会有所帮助。

您应该使用.NET版本类,并且使用CompareTo(Object)方法进行比较。

var input = new Regex(@"(?<=Version:)\s*(.*)").Matches(@"Name:   My Software
Version:  1.0.1
Installed location: c:\my folder")[0].Value.Trim();

var a = new Version(input);
var b = new Version("2.0.0");

int comparison = a.CompareTo(b);

if(comparison > 0)
{
    Console.WriteLine(a + " is a greater version.");
} 
else if(comparison == 0)
{
    Console.WriteLine(a + " and " + b +" are the same version.");
}   
else
{
    Console.WriteLine(b + " is a greater version.");
}
string versionText;
var stuff = tmp.Split(":");
if(stuff[0].Trim() == "Version")
{
    isAvailable = true;
    versionText = stuff[1].Trim();
}

if(versionText == expectedVersionText)  // Do something specfic.

尝试如下...它将为您提供帮助...

通过使用IndexOf而不是Contains检查单词。

if (tmp.IndexOf("Version") != -1)
{
isAvailable = true;
string[] info = tmp.Split(':');
string version = info[1].Trim();
Console.WriteLine(version);
}

您可能要使用正则表达式:

^Version:\s*(.*)$

应该与括号内的版本号匹配。

            string sought = "Version:";
            foreach (string tmp in lines)
            {
                if (tmp.Contains(sought))
                {
                    int position = tmp.IndexOf(sought) + sought.Length;
                    string version = tmp.Substring(tmp.IndexOf(sought) + sought.Length);
                    string[] versionParts = version.Split('.');
                    VersionCompare(versionParts, new string[]{"2", "0", "0"});
                }
            }
/// <summary>Returns 0 if the two versions are equal, else a negative number if the first is smaller, or a positive value if the first is larder and the second is smaller.</summary>
private int VersionCompare(string[] left, string[] right)
{
    for(int i = 0; i < Math.Min(left.Length, right.Length); ++i)
    {
        int leftValue = int.Parse(left[i]), rightValue = int.Parse(right[i]);
        if(leftValue != rightValue) return leftValue - rightValue;
    }
    return left.Length - right.Length;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM