【问题标题】:Creating a Windows auto restart + a counter that increments each reboot创建 Windows 自动重启 + 每次重启时递增的计数器
【发布时间】:2021-12-17 01:55:13
【问题描述】:

我正在尝试设置一个程序以在启动后自动重启窗口,并增加它已完成的重启次数。

我已经编写了少量代码,但是当它放在启动文件夹中时,它甚至没有重新启动系统本身。您可以非常短暂地看到命令提示符,然后什么也没有发生。

import java.awt.event.WindowEvent;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.io.*;
import javax.swing.*;


public class AutoRestart {

    public static void main(String[] args) throws IOException {
        int numRestarts = 0;

        Runtime r = Runtime.getRuntime();
        numRestarts++;


        File outFile = new File("C:\\reboots\\numberOfReboots.txt");
        if (outFile.exists()) {
            System.exit(0);
        }

        PrintWriter writer = new PrintWriter(outFile);
        writer.println("Number of times rebooted: " + numRestarts);
        writer.close();

        r.exec("shutdown -r -t 0");
        System.out.println("Restarting. . .");

    }

}

【问题讨论】:

  • 您的应用完全按照您编写的方式执行操作:它将numRestarts 递增到 1,这与您到目前为止已经完成了多少次重新启动毫无关系(变量,显然,将无法在重新启动后存活)。然后,您检查该文件是否存在,如果存在,您的应用程序是否存在。因此,它什么也不做。
  • 它设置为在 shell:startup 文件夹中自动运行,我的目标是让它在每次启动时递增,但是它需要将文件保存到 numberOfReboots.txt 然后我需要它会在下次运行时提取该数字并将 numRestarts 设置为该数字。

标签: java batch-file


【解决方案1】:

int 变量在重启后无法生存,您可以尝试从刚刚创建的文件中读取并增加其中的数字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多