I have created a temporary folder to keep some audio files. My intentions are to delete this temporary folder after joining the audio files into one file.
Here is the part of my code that creates temporary files inside directory directoryName
:
fileName = fileCounter.ToString() + ".wav";
fileCounter++;
fileName = directoryName + "\\" + fileName;
Code that joins all the temporary files is as follows:
public void Concatenate(string outputFile, List<string> sourceFiles)
{
byte[] buffer = new byte[1024];
WaveFileWriter waveFileWriter = null;
try
{
foreach (string sourceFile in sourceFiles)
{
using (WaveFileReader reader = new WaveFileReader(sourceFile))
{
if (waveFileWriter == null)
{
// first time in create new Writer
waveFileWriter = new WaveFileWriter(outputFile, reader.WaveFormat);
}
else
{
if (!reader.WaveFormat.Equals(waveFileWriter.WaveFormat))
{
throw new InvalidOperationException("Can't concatenate WAV Files that don't share the same format");
}
}
int read;
while ((read = reader.Read(buffer, 0, buffer.Length)) > 0)
{
waveFileWriter.WriteData(buffer, 0, read);
}
}
}
}
finally
{
if (waveFileWriter != null)
{
waveFileWriter.Dispose();
}
}
}
I had help from this question
Here is my code to delete the files
private void DeleteDirectory()
{
string[] files = Directory.GetFiles(directoryName);
foreach (string file in files)
{
File.Delete(file);
}
Directory.Delete(directoryName);
}
But the code in the question only deleted files inside the folder. But I want to delete the folder as well.