从一个文件夹复制文件到另一个文件夹

4

我正在开发一个网站,想要从应用程序文件夹中复制文件到服务器上的另一个文件夹(但该文件夹位于我的应用程序文件夹之外,即我的应用程序在C驱动器上,目标文件夹在D驱动器上)。是否可以使用Asp.Net的任何功能实现这一点?

提前感谢。


你试过了吗?只需要一行代码就可以测试它。 - Oded
@Oded,我不明白,你能解释一下吗? - gofor.net
1
看起来有点严厉的降级。我见过更糟糕的情况。 - Liam
1
@gofor.net,每当您提出问题时,请尝试将您的代码努力与问题一起放置,参考链接将使您的问题免受投票下降的影响。 - Devjosh
@gofor 不客气,亲爱的 - Devjosh
3个回答

10

是的,这是可能的,唯一需要注意的是CopyTo路径应该是完整路径,而不是相对路径(例如:c:\websites\myOtherFolder)。

这样,您就可以成功地从ASP.NET代码中复制/移动文件。

以下是伪代码,展示如何完成此操作(假设文件已经放置在ASP.NET应用程序的根目录中)。

 using System.IO;
    ..
    ..
    ..



// Get the current app path:
var currentApplicationPath =  HttpContext.Current.Request.PhysicalApplicationPath;

//Get the full path of the file    
var fullFilePath = currentApplicationPath + fileNameWithExtension;

// Get the destination path
var copyToPath = "This has to be the full path to your destination directory. 
                  Example d:\myfolder";

// Copy the file
File.Copy(fullFilePath , copyToPath );

1

使用此函数:

System.IO.File.Copy(FileToCopy, NewCopy)

0

将文件从一个文件夹移动到另一个文件夹非常容易。您可以在移动文件时更改文件名...

           string Tranfiles, ProcessedFiles;

           //Tranfiles = Server.MapPath(@"~\godurian\sth100\transfiles\" + Filename);

           Tranfiles = Server.MapPath(@"~\transfiles\" + Filename);
           if (File.Exists(Server.MapPath(@"~\transfiles\" + Filename)))
           {
               File.Delete(Server.MapPath(@"~\transfiles\" + Filename));
           }

           //ProcessedFiles = Server.MapPath(@"~\godurian\sth100\ProcessedFiles");
           ProcessedFiles = Server.MapPath(@"~\ProcessedFiles");

           File.Move(Tranfiles, ProcessedFiles);

这就是了,现在你可以检查应用程序文件夹以确认移动过程的状态


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接