4

How to get the network path of a file or directory on a windows pc using java? Usually we can see it in shared folder's properties on windows. As shown below..... enter image description here

Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
Imon
  • 3,905
  • 4
  • 25
  • 25

2 Answers2

2

Using Java you can pass net SHARE command in exec method of Runtime class and then parse the outputstream from the Process class to get the corresponding Network Path of your directory. The output of net SHARE directory_name command is as follows:

Share name        Share
Path              C:\Share
Remark
Maximum users     No limit
Users
Caching           Manual caching of documents
Permission        user, FULL

You need to get the value of the Path key from the above output. Following is the pseudo-code of how you can achieve this:

    Process process = Runtime.getRuntime().exec("net SHARE directory_name");
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    StringWriter writer = new StringWriter();
    String line;

    while (null != (line = reader.readLine())) {
        writer.write(line);
    }

    System.out.println(writer.toString());
Jugal Shah
  • 3,621
  • 1
  • 24
  • 35
  • Now it is running with no error, but where should I put the input directory? Does this work on Windows? I am trying with -- Runtime.getRuntime().exec("net SHARE E:\\"); with no luck. Thanks for your time. – Imon Oct 07 '11 at 08:46
0

The API to call is WNetEnumResource. You need to use JNI to call Windows APIs. An example can be found at http://public.m-plify.net/sourcecode/.

Sheng Jiang 蒋晟
  • 15,125
  • 2
  • 28
  • 46