2012-05-16 65 views
0

我想刪除名稱和包含文件夾的文件; 當我說的名字我的意思是沒有擴展名。通過名稱獲取文件擴展名

這是我知道的方式。 (對你有一點幫助,它會作品)

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

什麼時候是多個文件具有相同的名稱和不同的擴展名? –

+0

@Radu,你爲什麼不編輯SO用戶的格式? – Gabe

+0

@TimSchmelter我不給它是具有相同名稱的文件 – Noam650

回答

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

完成。

+0

但它不是一個txt文件。它是png/jpeg/jpg/gif文件 – Noam650