Deleting files on a remote computer

Can I connect to a remote computer and delete files from it? If not, are there other approaches that can solve this problem?

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

+2


source to share


3 answers


If you can achieve this via a standard network path, then this is the easiest way. \\machinename\c$\

... You must be an administrator on the machine to reach this path. This works with System.IO objects (File, Directory, etc.).



+2


source


If you cannot get to the network share, you can create a batch file for remote execution.

So remove.bat

c:\
cd \Windows\system32\example\
rm *

      

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



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

      

After the exit psexec bit is complete on the remote computer.

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

+4


source


If it's a computer on your local network or VPN, you can use a UNC path, but you need to know each username and password. You will need to use the WNetUseConnection Win32 API from C # to connect to your computer. Once connected, you can delete files in the usual way. Just provide the full UNC path to the file \\machine\c$\file.txt

.

If the machine is on a network rather than a local network or VPN, WCF sounds like you are looking for. You can create a service contract with a file delete operation. There is a huge introduction to WCF Services here .

+1


source







All Articles