C#检查是否安装了Java&&;版本(int)

C#检查是否安装了Java&&;版本(int),c#,C#,我正在为我的java游戏制作launcher,我想检查是否安装了Java8,因为若并没有安装java,就不能运行java应用程序。我在网上找到了答案,但找不到我问题的答案 我的当前代码: string temp = GetJavaVersion(); string temp2 = temp.Substring(13, temp.Length - 13); //string temp3 = temp2.Substring(10, temp2.Length - 1); string installP

我正在为我的java游戏制作launcher,我想检查是否安装了Java8,因为若并没有安装java,就不能运行java应用程序。我在网上找到了答案,但找不到我问题的答案

我的当前代码:

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);
        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"
GetJavaVersion()方法代码:

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);
        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"
输出:

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);
        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"
问题是我得到了引号和不必要的数字(我只需要数字8来表示安装了java版本8),若并没有安装java,那个么只输出0

有人能帮忙吗

编辑 如果我使用
Regex
,那么如何将number转换为int变量? 代码:


您可以使用正则表达式将您的版本拆分为数字:

string regexPattern = @"([0-9]+)";
Regex regex = new Regex(regexPattern);
这将返回一个包含4个结果的数组:

1
8
0
144

然后获取您的
8
版本,如
regex.Matches(“1.8.0_144”)[1]。值

要将其转换为int使用

int value = int.Parse(regex.Matches("1.8.0_144")[1].Value);

您还可以使用LINQ,因为正则表达式非常昂贵:

var result = "1.8.0_144";
var version = new string(result.SkipWhile(c => c != '.')
                               .Skip(1)
                               .TakeWhile(c => Char.IsDigit(c))
                               .ToArray());
Console.WriteLine(version); // 8

再次查看帖子,我在编辑答案时添加了新内容,您应该从
Match
中获得一个值,谢谢您的回复。