获取电脑信息(登录电脑的进程、C盘文件信息、浏览器信息、IP)

电脑的进程信息

		// 获取登录电脑的进程信息
        String os = System.getProperty("os.name").toLowerCase();
        String command;
        if (os.contains("win")) {
            command = "tasklist";
        } else {
            command = "ps -ef";
        }

        try {
            Process process = new ProcessBuilder(command.split(" ")).start();
            // 关键修改:指定 GBK 编码
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

C盘文件信息

        // 获取 C 盘第二级文件列表及内容
        if (os.contains("win")) {
            File cDrive = new File("C:\\");
            File[] firstLevelFiles = cDrive.listFiles();
            if (firstLevelFiles != null) {
                for (File firstLevelFile : firstLevelFiles) {
                    if (firstLevelFile.isDirectory() && !firstLevelFile.getName().startsWith("$")) {
                        System.out.println("一级文件夹: " + firstLevelFile.getName());
                        File[] secondLevelFiles = firstLevelFile.listFiles();
                        if (secondLevelFiles != null) {
                            for (File secondLevelFile : secondLevelFiles) {
                                if (!secondLevelFile.getName().startsWith("$")) {
                                    System.out.println("  二级文件/文件夹名称: " + secondLevelFile.getName());
                                    if (secondLevelFile.isFile()) {
                                        try {
                                            Path filePath = Paths.get(secondLevelFile.getAbsolutePath());
                                            // 指定 UTF-8 编码读取文件,可根据实际情况修改
                                            try (Stream<String> lines = Files.lines(filePath, StandardCharsets.UTF_8)) {
                                                lines.forEach(System.out::println);
                                            }
                                        } catch (java.nio.charset.MalformedInputException ex) {
                                            System.err.println("文件 " + secondLevelFile.getName() + " 的编码格式不匹配: " + ex.getMessage());
                                        } catch (IOException ex) {
                                            System.err.println("读取文件 " + secondLevelFile.getName() + " 时出错: " + ex.getMessage());
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

电脑名称、IP

            // 获取电脑名称和 IP 地址
            InetAddress localHost = InetAddress.getLocalHost();
            String computerName = localHost.getHostName();
            String computerIp = localHost.getHostAddress();
            System.out.println("电脑名称: " + computerName);
            System.out.println("电脑 IP 地址: " + computerIp);

获取电脑使用登录浏览器信息

// 获取默认浏览器信息
String browserInfo = getDefaultBrowserInfo();
System.out.println("默认浏览器: " + browserInfo);
private String getDefaultBrowserInfo() {
        String os = System.getProperty("os.name").toLowerCase();
        if (os.contains("win")) {
            return getDefaultBrowserOnWindows();
        } else if (os.contains("linux")) {
            return getDefaultBrowserOnLinux();
        }
        return "Unable to determine default browser due to unknown OS";
    }

    private String getDefaultBrowserOnWindows() {
        try {
            Process process = Runtime.getRuntime().exec("reg query HKCU\\Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\http\\UserChoice /v Progid");
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.contains("Progid")) {
                    String[] parts = line.trim().split("\\s+");
                    if (parts.length >= 3) {
                        String browserKey = parts[2];
                        switch (browserKey) {
                            case "ChromeHTML":
                                return "Google Chrome";
                            case "FirefoxURL":
                                return "Mozilla Firefox";
                            case "IE.HTTP":
                                return "Internet Explorer";
                            case "AppXq0fevzme2pys62n3e0fbqa7peapykr8v":
                                return "Microsoft Edge";
                            default:
                                return "Unknown Browser: " + browserKey;
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "Unable to determine default browser";
    }
     private String getDefaultBrowserOnLinux() {
        try {
            // 尝试读取 $BROWSER 环境变量
            String browserEnv = System.getenv("BROWSER");
            if (browserEnv != null && !browserEnv.isEmpty()) {
                return browserEnv;
            }

            // 尝试使用 xdg-settings 命令
            Process process = Runtime.getRuntime().exec("xdg-settings get default-web-browser");
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = reader.readLine();
            if (line != null) {
                if (line.contains("chrome")) {
                    return "Google Chrome";
                } else if (line.contains("firefox")) {
                    return "Mozilla Firefox";
                } else {
                    return "Unknown Browser: " + line;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "Unable to determine default browser";
    }

此代码只为样例,实际情况自己分析

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

姚尧垚(无心)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值