简体   繁体   中英

How to copy file from one dir to another under IIS root using asp.net web service?

I am developing asp.net web service using win7 IIs server.under IIs root dir there are two sub dir how can i copy file from one sub folder to another this my try:

    string fileName="file.txt";
    string sourcePath = @"localhost\C:\inetpub\wwwroot\source";
    string targetPath =  @"localhost\C:\inetpub\wwwroot\dest";


    string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
    string destFile = System.IO.Path.Combine(targetPath, fileName);


    System.IO.File.Copy(sourceFile, destFile, true);

thanks in advance

You should use physical paths. That is :

string fileName="file.txt";
string sourcePath = @"C:\inetpub\wwwroot\source";
string targetPath =  @"C:\inetpub\wwwroot\dest";   

string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);

System.IO.File.Copy(sourceFile, destFile, true);

If you need to involve the website root (in this case, folders on the site root folder), it should be like this:

string fileName="file.txt";
string sourcePath = Server.MapPath("/source");
string targetPath =  Server.MapPath("/dest");   

string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);

System.IO.File.Copy(sourceFile, destFile, true);

Best

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