简体   繁体   中英

command line output validation in c#

My output in the actual command prompt looks like this:

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

I am trying to get this output via c# code

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; 
    }
}

I don't want to just check a version tag, I am trying to get the version value and do a compare, for example if the value is 1.0.1, i would want that value and do a comparison with 2.0.0.

I can use indexof (like result.IndexOf("Version:"); ) - But that doesn't get me the value of the version

Any thoughts will be helpful.

You should use the .NET Version class and it's CompareTo(Object) method to do your comparison.

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.

Try like below... it will help you...

Instead of Contains check the word by using IndexOf ...

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

You might want to use Regular Expressions:

^Version:\s*(.*)$

should match the version number inside the parentheses.

            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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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