Avatar of yokai
yokai
 asked on

Copy Folder & Contents to Multiple Machines on Network

Good morning,

I would like to copy a folder & all contents of it (FOLDER) from a host machine (HOST) to multiple computers on a network.  If I have, say, 6 computers (COMPUTER1, COMPUTER2, etc), how can I copy FOLDER to the C: drive on these machines?  I am able to access the C drive of each machine via UNC \\COMPUTER1\C$.
C#.NET Programming

Avatar of undefined
Last Comment
rmindel

8/22/2022 - Mon
rmindel

try using DirectoryInfo, public DirectoryInfo.GetFiles, FileInfo ,public Fileinfo.CopyTo

something like this:

that doesnt include subfolders
in order to take care of subfolders you need to use DirectoryInfo..GetDirectories()
and make it recursive

Hope it's what you need
dirtarget = @"c:\test";
dirsource = @"\\COMPUTER1\C$\test";
 
DirectoryInfo source = new DirectoryInfo(dirsource);
DirectoryInfo target = new DirectoryInfo(dirtarget);
 
if (Directory.Exist(target.FullName) == false)
{
Directory.CreateDirectory(target.FullName);
}
 
foreach (FileInfo fi in source.GetFiles())
{
fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
}

Open in new window

yokai

ASKER
rmindel,
There are no subfolders, so that makes life a little easier.  However, is there a way that I can just add all the computer names to a text file or something and loop through it, so I don't have to hard code "dirsource"?
ASKER CERTIFIED SOLUTION
rmindel

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Your help has saved me hundreds of hours of internet surfing.
fblack61