当我运行asp.net网站时,如何编写代码删除文件夹中的所有文件? [英] how to write code for delete all files inside folder when i am running asp.net website?
本文介绍了当我运行asp.net网站时,如何编写代码删除文件夹中的所有文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尊敬的先生,
当我运行asp.net网站时,如何编写用于删除文件夹内所有文件的代码?
注意,它会动态运行并删除所有文件.
请给我一个解决方案?
通过mohan
Dear sir,
how to write code for delete all files inside folder when i am running asp.net website?
Note, Dynamically it run and delete all files.
please give me a solutions?
By mohan
推荐答案
您可以使用循环:
You can use a loop:
string[] filePaths = Directory.GetFiles(@"c:\MyDir\");
foreach (string filePath in filePaths)
{
File.Delete(filePath);
}
您可以将Array.ForEach与匿名方法一起使用:
You can use Array.ForEach with an anonymous method:
Array.ForEach(Directory.GetFiles(@"c:\MyDir\"), delegate(string path) { File.Delete(path); });
或者您可以使用
Or you could use
Directory.Delete(path, true);
Directory.CreateDirectory(path);
删除文件夹及其内容和所有子目录,然后重新创建文件夹.
to remove the folder, it''s contents, and all subdirectories, then re-create the folder.
public void DeleteAllTempFiles()
{
foreach (var f in System.IO.Directory.GetFiles(Server.MapPath("~/temp")))
System.IO.File.Delete(f);
}
首先要做这些事情,您应该具有正确的安全性并对该文件夹具有读/写权限.
然后,您可以尝试这段代码:
First of all to do such things you should have the right security and read/write permissions on that folder.
Then you can try this piece of code :
DirectoryInfo objDir = new DirectoryInfo(sPath);
//Check whether path exists
if (objDir.Exists)
{
FileInfo[] objFile = objDir.GetFiles();
for (int iIndex = 0; iIndex < objFile.Length; iIndex++)
{
File.Delete(objFile[0].DirectoryName + "\\" + objFile[0].Name);
}
}
这篇关于当我运行asp.net网站时,如何编写代码删除文件夹中的所有文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文