Verify that the dynamic name file exists on disk

2

My program generates an XML file with the following name:

  

C: /FileFile/NOMETER_DOM_Data_NrFeaturesNoXML.xml

At the moment, every time I generate a file and it has nrDeElementosNoXML other than nrDeElementosNoXML of the existing file, it creates me one more file. Can I use some method to delete the file that already exists before creating a new one? Something like this:

if(File.Exists(@"C:/caminhoDosFicheiro/NOMEQUENAOMUDA_data_*valorDesconhecido*.xml"))
{
    File.Delete(@"C:/caminhoDosFicheiro/NOMEQUENAOMUDA_data_*valorDesconhecido*.xml");
}
    
asked by anonymous 23.03.2017 / 12:37

1 answer

4

Perform the deletion using foreach using the desired filter on GetFiles :

System.IO.DirectoryInfo di = new DirectoryInfo(@"C:/caminhoDosFicheiro");

foreach (FileInfo file in di.GetFiles("NOMEQUENAOMUDA_data_*.xml"))
{
    file.Delete(); 
}

There are other ways too, you can check out this link (SOen).

    
23.03.2017 / 13:01