【发布时间】:2014-10-14 20:37:09
【问题描述】:
我正在使用 C# winform,我想在其中搜索文件夹中的特定文件并且我想删除它。 我怎样才能做到这一点。我正在尝试使用以下代码。
private void button4_Click(object sender, EventArgs e)
{
string Filename = img_path.Text; // here i have the filename "sample.grf"
if (Directory.GetFiles(@"E:\Debug").Where(x => x.Name == Filename).Any()) // getting error here
{
// i want to search here in above folder and delete the file.. how to do this
System.IO.File.Delete(/dont know how to delte the particular file);
}
}
请帮忙
【问题讨论】:
-
为什么不直接使用
File.Exists呢?还有你得到的错误是什么 -
Directory.GetFiles不会像您假设的那样返回FileInfo[],请使用if (new DirectoryInfo(@"E:\Debug").GetFiles().Where(x => x.Name == Filename).Any()),但您可以简单地使用if(File.Exists(Path.Combine(@"E:\Debug", Filename))) -
x 是一个字符串,所以可能 x == 文件名,我认为每个人都忽略了这一点,因为他可能不知道整个路径。只是文件名。
标签: c# visual-studio-2010 visual-studio