简体   繁体   中英

remove files remotely using c# and system.io.file

I am trying to remove a file located at a remote server named tn1pfe-01. But an exception is returned instead.

The given path's format is not supported

Here is my instruction:

File.Delete(@"\\\\"+"tn1pfe-01"+@"\\"+"C:\\\\Users\\\\skobbi\\\\Documents\\\\Fiddler2\\\\sa.txt" );

When you access a remote machine you cannot refer to the drive as C:\\\\ . Instead you need to use the hidden administrative share c$ , eg

\\svr\\c$\\Windows\\file.txt

So in your case, you will need

File.Delete(@"\\"+"tn1pfe-01"+@"\"+"c$\\Users\\skobbi\\Documents\\Fiddler2\\sa.txt");

If the file path if a variable containing a local path, you can generate this by replacing :\\ with $\\ :

string localPath = "c:\\Users\\skobbi\\Documents\\Fiddler2\\sa.txt";
File.Delete(@"\\" + "tn1pfe-01" + @"\" + localPath.Replace(@":\", @"$\"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM