按名称获取文件扩展名

时间:2012-05-16 15:26:58

标签: c# combobox io delete-file

我想删除一个带有名称和包含文件夹的文件; 当我说出名字时我的意思是没有扩展名。

这是我所知道的方式。 (在你的帮助下,它会起作用)

//the  extension string doesn't work properly.Here I need your help
string extension = Path.GetExtension(@"C:\Projects_2012\Project_Noam\Files\ProteinPic" + comboBox1.SelectedItem.ToString());
string fileLoc = @"C:\Projects_2012\Project_Noam\Files\ProteinPic\" + comboBox1.SelectedItem.ToString() + extension;
if (File.Exists(fileLoc))
{
    File.Delete(fileLoc);
}

2 个答案:

答案 0 :(得分:2)

您可以使用Directory.GetFiles并使用*代替扩展程序。

答案 1 :(得分:0)

作为代码的精确翻译,这就是我要写的内容:

string extension = @".txt"; // For example;
string basePath = "@C:\SomePath\";
string pathWithoutExtension = Path.GetExtension(Path.Combine(basePath, comboBox1.Text));
string fullPath = Path.ChangeExtension(pathWithourExtension, extension);

if (!File.Exists(fullPath))
    // Do stuff...
else
    // Do other stuff...

完成。