java - 通过修改desktop.ini来更改图标的文件夹

标签 java windows file attributes ini

我的目标是使用 Java 将电影库中每个文件夹的图标更改为 Windows 中每个文件夹的图标。

每个文件夹中都有一个以文件夹命名的 256x256 图标,但具有适当的扩展名。 例如,名为 5cm Per Second 的文件夹中包含文件 5cm Per Second.ico

我想我可以通过修改文件夹中的desktop.ini文件来做到这一点。所有文件夹中都包含该文件,因为每个文件夹内的图标曾经是该文件夹的实际图标,但在我更改了电影库的路径后,desktop.ini 中的图标路径没有更新。

每个desktop.ini看起来像这样:

[.ShellClassInfo]

IconResource=F:\Anime\Movies\5cm Per Second\5cm Per Second.ico,0

现在图标所在的路径是这样的:E:\Movies\5cm Per Second\5cm Per Second.ico 所以我想我所要做的就是更改 Desktop.ini 为:

[.ShellClassInfo]

IconResource=E:\Movies\5cm Per Second\5cm Per Second.ico,0

这根本不起作用,然后我想我还应该让 Windows 知道 desktop.ini 是一个系统文件,我将其添加到我的代码中,但根本不起作用。

我的示例文件夹代码:

import java.io.File;
import java.io.IOException;
import org.ini4j.Wini;

public class ListFiles {

    public static void main(String[] args) throws IOException {

        // Directory path here
        String path = "E:\\Movies\\5cm Per Second";

        String fileName;
        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();

        for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                if ("ini".equals(getExtension(listOfFiles[i])))
                {
                    File theFile = listOfFiles[i];
                    Wini ini = new Wini(theFile);
                    String iconPath = theFile.getParent() + ".ico";
                    String field = iconPath + ",0";
                    ini.put(".ShellClassInfo", "IconResource", field);
                    Runtime.getRuntime().exec("attrib +H " + theFile.getAbsolutePath());
                }
            }
        }
    }

    public static String getExtension(File theFile) {
        String extension = null;
        String fileName = theFile.getName();
        int i = fileName.lastIndexOf('.');

        if (i > 0 && i < fileName.length() - 1) {
            extension = fileName.substring(i + 1).toLowerCase();
        }

        if (extension == null) {
            return "";
        }
        return extension;
    }
}

如您所见,我将 IconResource 字段编辑为我需要使用 ini4j 库的字段,然后更改 desktop.ini 的属性 隐藏和系统。

看起来这还不够,我真的不知道还能做什么。

最佳答案

您没有调用ini.store() ,这可能会将更改写入磁盘。

关于java - 通过修改desktop.ini来更改图标的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12759709/

相关文章:

java - 在 Java 中反序列化包含 __type 而不是 @class 的 JSON

java - 读取网络共享上的文件

windows - libusb-win32 : Automated installation of a driver filter

C:在文件中查找重复的字符串值

java - 使用java从txt文件中删除最后一个空行

java - 什么 Java GUI 框架适合第一个 GUI 项目?

java - Apache POI 条件格式搜索以某些文本开头的字符串

c - 是否可以在 Windows 机器上为 Linux 编译 CUDA C 代码?

c - 修改命令行参数,使它们在 ps 输出中不可见

c# - 如何从文件中删除单个属性(例如只读)?