2013-04-03 192 views
-1

我在實際的命令提示符下輸出看起來是這樣的:命令行輸出驗證

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:");) - 但是,這並不讓我的版本

值的任何想法會有所幫助。

回答

1

嘗試像下面......它會幫助你...

相反Contains檢查這個詞的使用IndexOf ...

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

上面的代碼給了我版本值,所有答案都很有幫助,謝謝大家。 – Sharpeye500

1

您可能需要使用正則表達式:

^Version:\s*(.*)$ 

應該匹配括號內的版本號。

1
string versionText; 
var stuff = tmp.Split(":"); 
if(stuff[0].Trim() == "Version") 
{ 
    isAvailable = true; 
    versionText = stuff[1].Trim(); 
} 

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

您應該考慮使用' .Trim()去除空白。 – pascalhein

+0

@csharpler是的,這將是一個好主意。更新了答案。 –

2

您應該使用.NET Version class和它的CompareTo(Object) method做你的比較。

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."); 
} 
+0

1.0.1獲得這個價值是我面臨的挑戰。 – Sharpeye500

+0

@ Sharpeye500我用稍微修改了csharpler在他們的回答中使用的正則表達式對它進行了更新 –

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