如何读取文件流的名称 [英] How do I read the names of a filestream
本文介绍了如何读取文件流的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Server.MapPath(图片/)中存储了3张jpg图片。我需要一种方法来读取每个jpg图片名称,然后通过下面的文件流删除循环每个filejpg。
I have 3 jpg pictures stored in Server.MapPath("Pictures/"). I need a way to read each jpg picture name then loop each filejpg thru the filestream delete below.
try
{
using (FileStream fsSource = new FileStream(Server.MapPath("Pictures/" + filejpg), FileMode.Open, FileAccess.Read))
{
File.Delete(Server.MapPath("Pictures/" + filejpg));
}
}
catch (FileNotFoundException ioEx)
{
Label100.Text = "Filestream Delete Old: " + ioEx.ToString();
}
我的尝试:
我试过删除图片文件夹。这不起作用:使用(FileStream fsSource = new FileStream(Server.MapPath(Pictures /),FileMode.Open,FileAccess.Read))
What I have tried:
I tried just deleting the Pictures folder. This did not work: using (FileStream fsSource = new FileStream(Server.MapPath("Pictures/"), FileMode.Open, FileAccess.Read))
推荐答案
AFileStream
用于读取文件内容,而不是用于列出文件夹中的文件。
你当你打开FileStream
引用它时,无法删除文件。
如果你是尝试删除特定文件夹中的所有匹配文件,您需要使用 Directory.GetFiles [ ^ ]:
AFileStream
is used for reading the contents of a file, not for listing the files in a folder.
You can't delete a file whilst you've got an openFileStream
referencing it.
If you're trying to delete all matching files in a specific folder, you need to use Directory.GetFiles[^]:
string folder = Server.MapPath("~/Pictures/");
foreach (string file in Directory.GetFiles(folder, "*.jpg"))
{
try
{
File.Delete(file);
}
catch (IOException ex)
{
Label100.Text = string.Format("Delete '{0}': {1}", Path.GetFileName(file), ex);
}
}
这篇关于如何读取文件流的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文