How to search for shared folders in LAN using delphi

john-6789
john-6789 used Ask the Experts™
on
Hello,,

i need to search the local network for search and list the shared folders,
and how to copy them to my pc.

best regards,

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Emmanuel PASQUIERFreelance Project Manager
Top Expert 2010

Commented:
You can check in the list of all network resources available, here is how to get that list

Call it :
EnumNetworkResources(nil, lbNetworkDrive.Items );
procedure EnumNetworkResources(NetResource: PNEtResource; List: TStrings);
var
 EnumHandle, Count, BufSize, I: Cardinal;
 NetArray: array[0..250] of TNetResource;
begin
 if WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, NetResource,EnumHandle) = NO_ERROR then
  try
   Count := $FFFFFFFF;
   BufSize := SizeOf(NetArray);
   if WNetEnumResource(EnumHandle, Count, @NetArray, BufSize) = NO_ERROR then
    begin
     for i := 0 to Count -1 do
      begin
       if NetArray[i].dwType=RESOURCETYPE_DISK Then List.Add(NetArray[i].lpRemoteName);
       EnumNetworkResources(@NetArray[i], List);
      end;
    end;
  finally
   WNetCloseEnum(EnumHandle);
  end;
end;

Open in new window

Commented:
please download the project file I attached and try
SearchShareNetworkList.zip
Emmanuel PASQUIERFreelance Project Manager
Top Expert 2010

Commented:
Hi systan ! How goes

Don't you find it a bit rude to kill the application if there is no shared folders ?
in NetCheck :
if csl<=0 then application.Terminate; 

Open in new window


Another note, your code would gain a lot by being indented.
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
oh, hi epasquier, glad you've talk
am, actually it depends on the user where and when they want to terminate
but i think the project pointed the right direction
indented? yep, it can be change.
thanks
Emmanuel PASQUIERFreelance Project Manager
Top Expert 2010

Commented:
indented? yep, it can be change.

Open in new window

Well, it's more a counsel for you, as if you get that habit to indent properly the code, you'll see that your skills will improve dramatically as it has a strong effect on how you can organize your code, and how fast you can travel through it to add features.

And for expert exchange, it helps a lot other readers
Geert GOracle dba
Top Expert 2009

Commented:
>systan
ouch my eyes, but you was expecting,
i'm not givin any comments on that collection of letters and numbers which are supposed to be code
Emmanuel PASQUIERFreelance Project Manager
Top Expert 2010

Commented:
Geert : you are lacking a bit of education, this is not a way to talk to ladies ;o)
but since you are only emphasizing my thoughts, I'll forgive you.

Commented:
hi guys
do you know, that im far away from both of you.
im trying to get points here, lol
you know i struggle to become a delphi guru, because im lazy and forgetful.
anyway,  forgive the way i code,   time will come, ill be like both of you.
most important in this post,  the asker got what it needs.

Author

Commented:
systan:

the example project work ok but only for winxp , the process terminated afiter a while in win7.

Commented:
Ok, try to remove this line,
if csl<=0 then application.Terminate;

Commented:
hi
I have borrowed a pc with windows 7 x86 32bit, I have no problem viewing the shared folders on the network.
I also remove that line.
Freelance Project Manager
Top Expert 2010
Commented:
Have you tried my function ?
I have thought first that you where complaining about the fact that WNetOpenEnum can sometime *freeze* 30-60 seconds on some net resources, when a server known by your workstation is offline at the moment.
So I tried to improve it with forward detection of network availability, with little luck. In fact, all I could find on the subject report the same inability of Windows Net API to do that quickly, pretty much the same as you experience in Explorer when you inadvertently click on an offline server - you end up waiting it gets to its *brain* that the server is unavailable, without much possibility to do anything else...

So I have tried WNetGetResourceInformation on each server I find in the way, before calling WNetOpenEnum on it , but it takes the same time.
MultinetGetConnectionPerformance function looked promising as well, but I couldn't make it work, besides its simple interface, and about no code sample in C, C++, VB or .Net could be found using it successfully. Not even in MSDN. So I guess this is just a remain of MS willing to do something about this problem, but they never did.

All in all, I conclude that the delay when one (or more) servers are offline is unavoidable, except by some methods I've seen here and there that implement a thread to call WNetEnumResource, that is killed and reported unavailable if takes more than 5 seconds.

Nevertheless, I've improved my function just a little bit in the process (after removing all my attempts, I pretty much go back to what I started with, except one little test to avoid unnecessary Enum in some cases)
procedure EnumNetworkResources(NetResource: PNEtResource; List: TStrings);
var
 EnumHandle, Count, BufSize, I: Cardinal;
 NetArray: array[0..250] of TNetResource;
begin
 if WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, NetResource,EnumHandle) = NO_ERROR then
  try
   Count := $FFFFFFFF;
   BufSize := SizeOf(NetArray);
   if WNetEnumResource(EnumHandle, Count, @NetArray, BufSize) = NO_ERROR then
    begin
     for i := 0 to Count -1 do
      begin
       if NetArray[i].dwType=RESOURCETYPE_DISK Then List.Add(NetArray[i].lpRemoteName);
       if (NetArray[i].dwUsage And RESOURCEUSAGE_CONTAINER)>0 Then EnumNetworkResources(@NetArray[i], List, ListErrors);
      end;
    end;
  finally
   WNetCloseEnum(EnumHandle);
  end;
end;

Open in new window

Commented:
change from:
if (NetArray[i].dwUsage And RESOURCEUSAGE_CONTAINER)>0 Then EnumNetworkResources(@NetArray[i], List, ListErrors);

Open in new window

to:
if (NetArray[i].dwUsage And RESOURCEUSAGE_CONTAINER)>0 Then EnumNetworkResources(@NetArray[i], List);

Open in new window

Emmanuel PASQUIERFreelance Project Manager
Top Expert 2010

Commented:
my bad, I forgot to remove something along the way. Thank you for noting it, systan

Commented:
Men, I though this was closed
Emmanuel PASQUIERFreelance Project Manager
Top Expert 2010

Commented:
Well, at least there is not much more to say or code to write
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Commented:
thank you

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial