JavaがインストールされていないとJavaアプリケーションを実行できないため、私のJavaゲームのランチャーを作っています。私はインターネット上の答えを見て、私の質問に答えを見つけることができません。C#Javaがインストールされているかどうかを確認します。&& version(int)
私の現在のコード:
string temp = GetJavaVersion();
string temp2 = temp.Substring(13, temp.Length - 13);
//string temp3 = temp2.Substring(10, temp2.Length - 1);
string installPath = temp2;
ShowMessageBox(installPath, "Debug", MessageBoxButtons.OK,
MessageBoxIcon.Information);
GetJavaVersion()メソッドのコード:
try
{
ProcessStartInfo procStartInfo =
new ProcessStartInfo("java", "-version ");
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
return proc.StandardError.ReadLine();
}
catch (Exception objException)
{
return null;
}
出力:
"1.8.0_144"
問題は私が引用符と不要な番号を取得することです(私はちょうどjavaバージョン8がインストールされていることを示す番号8が必要です)、javaがインストールされていない場合0を出力します。
Can Any One Help?
Regex
を使用すると、数値をint変数に変換する方法はありますか? コード:
string temp = GetJavaVersion();
string temp2 = temp.Substring(13, temp.Length - 13);
requiredJavaVersion = 8;
//string temp3 = temp2.Substring(10, temp2.Length - 1);
string regexPattern = @"([0-9]+)";
Regex regex = new Regex(regexPattern);
//Error comes here
int currentVersion = Convert.ToInt32(regex.Matches("1.8.0_144")[1]);
if (currentVersion == requiredJavaVersion)
{
hasRequiredVersion = true;
}
が再びポストをチェックし、私はEDIT – MemesTV
編集答え、あなたに新しいものを追加しました'Match 'から値を取得する必要があります。 –
Anwserに感謝します。 – MemesTV