Changing folder icon with ini4j gives access denied exception

793 Views Asked by At

I am trying to change folder icons using ini4j.

I have the following file structure

  • F:\TBD\ChangeICon\Icons\avi.ico - (a folder with icons in)
  • F:\TBD\ChangeICon\Me\desktop.ini - (a folder I need to change the icon)

The desktop.ini has the following data:

[.ShellClassInfo]
IconResource=C:\Windows\system32\SHELL32.dll,4

I hardcoded the icon and folder in the java method below and if I copy the filename and icon path strings I can access it by going the windows start >> run. I don’t believe the paths are wrong.

When I run the application at ref 1 (see ref comments in code), if I inspect the value of ini (at REF 1):

{.ShellClassInfo=[{IconResource=[C:Windowssystem32SHELL32.dll,4]}]}

Again this match what the values is in the ini file above.

And then I try to set the value (REF2) I can see that the value in fact did change

{.ShellClassInfo=[{IconResource=[F:/TBD/ChangeICon/Icons/avi.ico,0]}]}

but when the store method is called it throws an exception (REF3

private boolean setIniValue2() {
    final String filename = "F:/TBD/ChangeICon/Me";
    final String icon = "F:/TBD/ChangeICon/Icons/avi.ico";
    try {           
        Ini ini = new Ini(new File(filename + "/" + "desktop.ini"));    // REF1        
        ini.put(".ShellClassInfo", "IconResource", icon + ",0");        // REF2 
        ini.store();
        return true;
    } catch (FileNotFoundException e) {                                 // REF3
        e.printStackTrace();
        throw new RuntimeException("Error finding file: " + filename + " when trying to change icon", e);
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Error accessing file: " + filename + " when trying to change icon", e);
    } 
}

The exception:

java.io.FileNotFoundException: F:\TBD\ChangeICon\Me\desktop.ini (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at org.ini4j.Ini.store(Ini.java:144)
at org.ini4j.Ini.store(Ini.java:129)
at za.co.chrispie.io.wrappers.ini.ini4JWrapper.setIniValue2(ini4JWrapper.java:48)
at za.co.chrispie.io.wrappers.ini.ini4JWrapper.setFolderIcon(ini4JWrapper.java:21)
at za.co.chrispie.TestChangeIcon.testChangeFolderIcon(TestChangeIcon.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

I can open up the desktop.ini file in notepad and edit it to be as below and save and the icon changes.

[.ShellClassInfo]
IconResource=F:\TBD\ChangeICon\Icons\avi.ico,0

Why does the java program don't have access to change the ini file?

1

There are 1 best solutions below

1
On

This is not a problem of java. It is a problem of windows.

I believe that you are running java from command prompt and probably it has different security priviladges than your notepad. Try to run program as administrator. I am not on windows now, so I cannot check it but as far as I remember there is command ranas in windows shell.