これまでのところ私のコードは次のとおりです。はい、それはかなり基本的なものです。しかし、私が達成したいのは、System32\drivers\etc フォルダーにあるホスト ファイルを置き換えることです。これを行う際の問題は、アクセス拒否エラーが発生することです。Windowsフォルダー内で変更を行うためのプログラムアクセスをどのように許可できますか?また、クラスSaveURLはファイルをダウンロードするだけで、2つの文字列を使用して関数saveImageを介して名前とダウンロード場所を渡します. 自分自身にアクセス権を付与するにはどうすればよいですか? よろしくお願いします。
private void jButton7MouseClicked(java.awt.event.MouseEvent evt) {
String destinationHosts = "hosts";
String urlHosts = "https://dl.dropbox.com/s/awdvoprxyo7r2q6/hosts?dl=1";
Object hostOptions[] = {"Replace", "Close"};
File fileHosts = new File(destinationHosts);
File dirHost = new File("hosts");
boolean dirHosts = dirHost.mkdir();
try {
Runtime.getRuntime().exec("notepad.exe \\Windows\\system32\\drivers\\etc\\hosts");
jTextArea2.append("Opening Host File \n");
int hostFile = JOptionPane.showOptionDialog(rootPane, "Would you like to replace the Hosts File?",
"Yes or No", JOptionPane.YES_NO_OPTION, JOptionPane.NO_OPTION, null, hostOptions, hostOptions[0]);
if (hostFile == JOptionPane.YES_NO_OPTION) {
jTextArea2.append("Downloading New Host File");
SaveUrl.saveImage(urlHosts, destinationHosts);
fileHosts.renameTo(new File(dirHost, fileHosts.getName()));
Path source = Paths.get("\\hosts\\hosts");
Path target = Paths.get("\\Windows\\system32\\drivers\\etc\\");
Files.copy(source, target);
jTextArea2.append("Host file replaced");
}
} catch (IOException ex) {
Logger.getLogger(ToolKit1.class.getName()).log(Level.SEVERE, null, ex);
}
}