0

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.

EpicKip
  • 4,015
  • 1
  • 20
  • 37
Babar Baig
  • 383
  • 2
  • 15
  • 1
    As far as I know, you can use Directory.Delete with true to delete all included files and subdirectories and don't need the loop. – o_weisman Jun 15 '17 at 07:23
  • Could have easily had found the answer at: https://stackoverflow.com/questions/1288718/how-to-delete-all-files-and-folders-in-a-directory – Azaz ul Haq Jun 15 '17 at 07:31

4 Answers4

2

You don't need to delete all the files in the folder before deleting it. Directory.Delete(directoryName,true) should delete the directory and all the files/subdirectories in it.

Anyway, you should enclose this in a try/catch, as if any of the files inside is in use it would throw an IOException

Pikoh
  • 7,582
  • 28
  • 53
  • This method will throw `Exception` if any of the File is in use. This need to take care to avoid exception – Gaurav P Jun 15 '17 at 07:26
  • You are right @GauravKP. Edited to show that possibility – Pikoh Jun 15 '17 at 07:28
  • @Pikoh You are correct `Directory.Delete(directoryName,true)` deletes all the files and folder in it. But it only deletes the contents of directoryName. I want to delete the directoryName as well. – Babar Baig Jun 15 '17 at 07:37
  • No @Code. `Directory.Delete` removes the folder as well – Pikoh Jun 15 '17 at 07:38
1

C#'s Directory.Delete has a recursive flag which you can use to delete the directory and all its contents. Using this you don't have to manually delete all the files first.

Simply pass true as the second argument to Directory.Delete:

Directory.Delete(directoryName, true);        

Use with caution!

TobiV
  • 108
  • 8
0

(This is unrelated to your question, but I'm posting as an answer because it's too unwieldy for a comment.)

The Concatenate method's code can be simplified and reduced by half:

public void Concatenate(string outputFile, List<string> sourceFiles)
{
    using (WaveFileWriter waveFileWriter = new WaveFileWriter(outputFile, reader.WaveFormat))
    {
        foreach (string sourceFile in sourceFiles)
        {
            using (WaveFileReader reader = new WaveFileReader(sourceFile))
            {
                if (reader.WaveFormat.Equals(waveFileWriter.WaveFormat))
                    reader.CopyTo(waveFileWriter);
                else
                    throw new InvalidOperationException("Can't concatenate WAV Files that don't share the same format");
            }
        }
    }
}
Chris R. Timmons
  • 2,187
  • 1
  • 13
  • 11
0

Consider using System.IO.Path class to create your temporary folder.

Also consider using DirectoryInfo and FileInfo for your files and folders instead of strings. After all: a file is not a string. A file has a name which is a string. Besides using DirectoryInfo and FileInfo makes sure there are no problems with the names of the files if you want to create / rename / move / delete files and folders or change extensions

var tmpFolderName = Path.Combine(Path.GetRandomPathName, Path.GetRandomFileName));
DirectoryInfo tmpFolder = Directory.Create(tmpFolderName)

// Tmp file name of a file in tmpFolder:
var tmpFileName = Path.Combine(tmpFolder.Name, fileName);

// Delete the tmp folder and everything in it:
tmpFilder.Delete(true);

This folder can be created, filled and deleted without any worries, because you are certain that no one will be using it, as no one has your folder name.

Using Path.Combine makes sure you don't have to worry about slashes '\'. Besides your code will be portable to be used on other systems, that don't use backslashes as separator.

Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116