5

I want to access drives/folders/sub-folders/files on remote machines over network for a machine in C#.

I know of a using WMI for this. However WMI might not be executable over remote machines due to security permissions.

What are alternative way in C# to enumerate the folders/subfolders/files for remote machines over the network.

Thanks! Gagan

2
  • By "remote machines", do you mean file shares (accessible by UNC paths, like \\machine\share\file.txt), FTP sites, or anything else? Commented Jun 20, 2016 at 6:49
  • Yes and paths like \\machine\C$. Is it possible to enumerate the folders and files on a network machine if we are provided the credentials of the network machine.
    – Gags
    Commented Jun 21, 2016 at 7:01

2 Answers 2

8

Shared folders on a UNC path can be enumerated just like local directories, using the classes in the System.IO namespace, like Directory.EnumerateFiles.

foreach (var file in Directory.EnumerateFiles(@"\\machine\c$"))
{
}

However, as you say, there's a question of credentials. If you need to specify different credentials to access the shares, you'll have to authenticate against the remote machine. Luckily, there's a great solution in this answer for creating ad-hoc network connections:

using (new NetworkConnection("\\machine\c$", new NetworkCredential("username", "password")))
{
    foreach (var file in Directory.EnumerateFiles(@"\\machine\c$"))
    {
    }
}
0
0

You can use forfiles, i am assuming all the machines are Windows.

Refer to this link for more information: Forfiles

Not the answer you're looking for? Browse other questions tagged or ask your own question.