Java: list files & folders for computers in LAN

2.3k Views Asked by At

Assume we have a PC in our LAN: 192.168.1.122 and it shares only ONE folder 'Shared' with ONE file 'Test.txt' (i.e. \\192.168.1.122\Shared\Test.txt). Now if I want to browse files and folders in \Shared, I write:

File f = new File("\\192.168.1.122\Shared");
File [] list = f.listFiles(); // Here I've got one item - Test.txt file. OK.

But when I try to browse the top level files & folders...

File f = new File("\\192.168.1.122");

... I get nothing in the list [] array. Please, help me with browsing root folders for the LAN device. Thank you!

1

There are 1 best solutions below

0
On

Expanding from this answer, as vcetinick suggested, you can use SmbFile.list() from jCIFS to list (according to the manual)

all browseable shares of a server including printers, IPC services, or disk volumes if this resource is a server URL in the form smb://server/,

which is what you've been asking. Using jCIFS this might even work when the application is not run on Windows but instead on an operating system where share access doesn't take the form of \\host\share\path.