删除特定文件夹及其文件ASP.NET [英] Deleting a specific folder and it's files ASP.NET

查看:230
本文介绍了删除特定文件夹及其文件ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我在这里有一个问题。我正在尝试使用ASP.NET(C#)删除Web服务器上另一个文件夹内的特定文件夹。要删除的文件夹基于文本框。

Alright so I'm having a bit of an issue here. I'm trying to delete a specific folder inside another folder on my webserver using ASP.NET (C#) The folder being deleted is based on a textbox.

目录为像这样

/images/folderx

folderx = txtDelFolder.Text;

问题是我尝试的所有操作都会删除图像文件夹中的所有内容。我猜想它不能识别文件路径中的文件夹

The problem is that everything I try deletes every single thing inside the images folder. I'm guessing that it is not recognizing my folder in the filepath


字符串路径= @ \httpdocs\images\ +
txtDelFolder.Text;

string path = @"\httpdocs\images\ + txtDelFolder.Text;

我也尝试过


字符串路径= @ \httpdocs\images\ +
txtDelFolder.Text + \;

string path = @"\httpdocs\images\ + txtDelFolder.Text + "\";

使用单一个'\'和双'\'

在此感谢您的任何帮助

也说< directfilepath> 我实际上输入了文件路径出来,只是不想在这里分享。

Also where it says <directfilepath> I actually have the filepath typed out, just didn't want to share that here.

**** edit ****

****edit****

string path = Server.MapPath("~/imagestest/" + txtEditTitle.Text);

  if(Directory.Exists(path)) 
  { 
  DeleteDirectory(path); 
  } 
 } 
} 
private void DeleteDirectory(string path) 
{ 
 foreach(string filename in Directory.GetFiles(path)) 
 { 
 File.Delete(filename); 
 } 
 foreach(string subfolders in Directory.GetDirectories(path)) 
 { 
 Directory.Delete(subfolders, true); 
 } 
}


推荐答案

尝试

private void DeleteFiles(string folder)
        {
            string path=Server.MapPath("~/httpdocs/images/" + folder);
            string[] files=Directory.GetFiles(path, "*", SearchOption.AllDirectories);
            foreach (string file in files)
            {
                File.Delete(file);
            }
             //then delete folder
              Directory.Delete(path);

        }

这篇关于删除特定文件夹及其文件ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆