当前位置: 首页 > news >正文

获取电脑信息(登录电脑的进程、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";}

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


http://www.mrgr.cn/news/99328.html

相关文章:

  • 常见的页面报错
  • 【前端Skill】点击目标元素定位跳转IDE中的源代码
  • VS Code + GitHub:高效开发工作流指南
  • 前端基础之《Vue(7)—生命周期》
  • aws服务(四)文件存储服务S3 介绍使用代码集成
  • 鸿蒙NEXT开发键盘工具类(ArkTs)
  • Mac idea WordExcel等文件git modify 一直提示修改状态
  • 【MySQL数据库入门到精通-02 SQL分类以及DDL操作】
  • n8n 中文系列教程_05.如何在本机部署/安装 n8n(详细图文教程)
  • git比较不同分支的不同提交文件差异
  • Java--数组的应用
  • 深入理解设计模式之模板方法模式
  • 数仓面试内容
  • Spring AI MCP
  • 字符串拼接问题的最佳解决方案
  • MetaGPT智能体框架深度解析:记忆模块设计与应用实践
  • C语言高频面试题——常量指针与指针常量区别
  • 堆栈溢出 StackOverflowError 排查
  • 辛格迪客户案例 | 浙江高跖医药委托生产质量管理协同(OWL MAH)项目
  • vue3 + element-plus中el-dialog对话框滚动条回到顶部