使用 cacls 锁定文件夹不起作用
Locking folder with cacls not working
所以我一直在尝试制作一个可以锁定文件夹的应用程序。在我添加一个 jtextfield 之前,它工作得很好,您可以在其中指定文件夹的路径和文件夹的名称。
这是锁定和解锁文件夹的数组:
String[] lock = new String[]{"cmd.exe", "/C", "cd \"" + String.valueOf(path.getText())+ "\" && dir", "&", "cacls", String.valueOf(name.getText()), "/e", "/c", "/d", "%username%"};
String[] unlock = new String[]{"cmd.exe", "/C", "cd \"" + String.valueOf(path.getText())+ "\" && dir", "&", "cacls", String.valueOf(name.getText()), "/e", "/c", "/g", "%username%:f"};
这是两个按钮的动作侦听器:
block.addActionListener(e -> {
if (password.getText().toString().equals(passwordText)) {
try {
Process p = Runtime.getRuntime().exec(lock);
} catch (IOException e1) {
e1.printStackTrace();
}
} else {
password.setText("");
passwordLabel.setText("Incorrect password");
failed.get().start();
}
});
unblock.addActionListener(e -> {
if (password.getText().toString().equals(passwordText)) {
try {
Process p = Runtime.getRuntime().exec(unlock);
} catch (IOException e1) {
e1.printStackTrace();
}
} else {
password.setText("");
passwordLabel.setText("Incorrect password");
failed.get().start();
}
});
当我直接指定路径和名称时,它工作正常,但现在它什么也没做。 (抱歉我的英语不好。请随时纠正我)
您的错误是初始化变量 lock
和 unlock
之前 JTextFields 中有一个有效值(当时,字段值是空字符串或 null)。相反,您应该在调用 Runtime.exec
.
之前初始化它们
所以我一直在尝试制作一个可以锁定文件夹的应用程序。在我添加一个 jtextfield 之前,它工作得很好,您可以在其中指定文件夹的路径和文件夹的名称。
这是锁定和解锁文件夹的数组:
String[] lock = new String[]{"cmd.exe", "/C", "cd \"" + String.valueOf(path.getText())+ "\" && dir", "&", "cacls", String.valueOf(name.getText()), "/e", "/c", "/d", "%username%"};
String[] unlock = new String[]{"cmd.exe", "/C", "cd \"" + String.valueOf(path.getText())+ "\" && dir", "&", "cacls", String.valueOf(name.getText()), "/e", "/c", "/g", "%username%:f"};
这是两个按钮的动作侦听器:
block.addActionListener(e -> {
if (password.getText().toString().equals(passwordText)) {
try {
Process p = Runtime.getRuntime().exec(lock);
} catch (IOException e1) {
e1.printStackTrace();
}
} else {
password.setText("");
passwordLabel.setText("Incorrect password");
failed.get().start();
}
});
unblock.addActionListener(e -> {
if (password.getText().toString().equals(passwordText)) {
try {
Process p = Runtime.getRuntime().exec(unlock);
} catch (IOException e1) {
e1.printStackTrace();
}
} else {
password.setText("");
passwordLabel.setText("Incorrect password");
failed.get().start();
}
});
当我直接指定路径和名称时,它工作正常,但现在它什么也没做。 (抱歉我的英语不好。请随时纠正我)
您的错误是初始化变量 lock
和 unlock
之前 JTextFields 中有一个有效值(当时,字段值是空字符串或 null)。相反,您应该在调用 Runtime.exec
.