【问题标题】:How to check if a particular folder or file is shared or not with java program?如何检查特定文件夹或文件是否与 java 程序共享?
【发布时间】:2013-11-02 18:35:17
【问题描述】:

我正在开发一个 java 网络项目。我的一个模块需要知道特定文件夹或文件是否共享,以及是否共享给谁。我的意思是它是与网络上的每个人共享还是仅与某些特定的人共享,因为我们在 Windows 中可以选择。这也必须是文件的一个属性,但我找不到任何方法来检查这个。

【问题讨论】:

  • 您要么需要通过 JNI 访问 Windows API,要么通过 Runtime.exec 调用外部程序并解析其输出。

标签: java windows file networking io


【解决方案1】:

您可以使用文件的exists方法来判断该目录是否为共享文件夹。

试试这个:

public static void main(String[] args) throws UnknownHostException {
    InetAddress addr;
    addr = InetAddress.getLocalHost();
    String hostname = addr.getHostName();

    if (hostname != null) {
        File f = new File("\\\\" + hostname + "\\temp");            
        if (f.exists()) {
            System.out.println("directory temp is shared");
        }           

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2017-05-14
    • 2018-11-17
    • 2015-10-18
    • 1970-01-01
    • 2013-06-08
    相关资源
    最近更新 更多