-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(like )を使用できますresult.IndexOf("Version:");- しかし、それではバージョンの値がわかりません

どんな考えも役に立ちます。

4

5 に答える 5

2

比較を行うには、.NET Version クラスとその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.");
}
于 2013-04-03T17:07:32.133 に答える
1

以下のようにしてみてください...それはあなたを助けます...

Containsを使用して単語をチェックする代わりにIndexOf...

if (tmp.IndexOf("Version") != -1)
{
isAvailable = true;
string[] info = tmp.Split(':');
string version = info[1].Trim();
Console.WriteLine(version);
}
于 2013-04-03T17:11:16.600 に答える
0

正規表現を使用したい場合があります。

^Version:\s*(.*)$

かっこ内のバージョン番号と一致する必要があります。

于 2013-04-03T17:01:45.250 に答える