Java File Path Delete deleteKey(String path, String key)

Here you can find the source of deleteKey(String path, String key)

Description

Deletes a registry key from the Windows registry.

License

Open Source License

Exception

Parameter Description
IOException an exception

Declaration

public static final void deleteKey(String path, String key) throws IOException 

Method Source Code


//package com.java2s;
/*//from   www  .  j  a va2s  . c  o  m
 * Syncany, www.syncany.org
 * Copyright (C) 2011-2015 Philipp C. Heckel <philipp.heckel@gmail.com> 
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Main {
    /**
     * Deletes a registry key from the Windows registry.
     * 
     * <p>This method executes the following command (example):
     * <pre>
     * $> reg delete /f HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v Syncany
     * The operation completed successfully.
     * </pre>
     * 
     * @throws IOException 
     */
    public static final void deleteKey(String path, String key) throws IOException {
        Objects.requireNonNull(path);

        // Build command
        List<String> command = new ArrayList<>();
        command.add("reg");
        command.add("delete");
        command.add(path);

        if (key != null) {
            command.add("/v");
            command.add(key);
        } else {
            command.add("/ve");
        }

        command.add("/f");

        // And run it
        try {
            Process regProcess = Runtime.getRuntime().exec(command.toArray(new String[0]));

            throwAwayStream(regProcess.getInputStream());
            throwAwayStream(regProcess.getErrorStream());

            regProcess.waitFor();
        } catch (InterruptedException e) {
            throw new IOException(e);
        }
    }

    private static void throwAwayStream(InputStream inputStream) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

        while ((reader.readLine()) != null) {
            // Do nothing.
        }
    }
}

Related

  1. deleteFolderRecursively(File path, boolean includeSelf)
  2. deleteIfExist(String filePath)
  3. deleteInPathTempFiles(String workingFolder)
  4. deleteInRemote(String host, String path)
  5. deleteJaasFile(String jaasPath)
  6. deleteMultipleFiles(String filePath, int numberOfFiles)
  7. deleteNonEmptyDirectory(String dirPath)
  8. deleteOldStorageFile(String filepath)
  9. deleteParent(String filePath)