繁体   English   中英

按名称获取文件扩展名

[英]Get file extension by its name

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

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

//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);
}

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

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

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...

完成。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM