Deleting files on a remote machine



Is it possible to connect to a remote machine and delete files from it? If not, are there any other approaches that can achieve this task?

One thought is that I could create a service that runs on each machine, implement a method that deletes local files, and then send commands to that service.

3 ответов


If you can reach it through a standard unc network path, then that is the easiest way. \machinename\c$\. You have to be an admin on the machine to reach that path. This works with the System.IO objects (File, Directory, etc.)


If you can't get to the network share, you could create a batch file to execute remotely.

So remove.bat

cd \Windows\system32\example\
rm *

and use psexec to copy the bat to the remote computer and execute it there.

psexec \example-computer -f -c remove.bat

After psexec exits, the bat has completed running on the remote computer.

psexec uses netbios, so you should be able to initiate a netbios session to the remote computer.


If it's a machine on your LAN or VPN, you can use a UNC path, but you'll have to know each machine's username and password. You would need to use the WNetUseConnection Win32 API from C# to connect to the computer. Once connected you can delete files using the normal method. Just specify the full UNC path of the file \machine\c$\file.txt.

If the machine is across the network and not on your LAN nor VPN, WCF sounds like what you're looking for. You can create a service contract with an operation of delete file. There is a great introduction to WCF services here.