Babar Baig
Babar Baig

Reputation: 383

Delete a folder after deleting its content using c#

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.

Upvotes: 0

Views: 375

Answers (4)

Harald Coppoolse
Harald Coppoolse

Reputation: 30454

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.

Upvotes: 0

Chris R. Timmons
Chris R. Timmons

Reputation: 2197

(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");
            }
        }
    }
}

Upvotes: 0

Pikoh
Pikoh

Reputation: 7703

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

Upvotes: 2

TobiV
TobiV

Reputation: 108

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!

Upvotes: 1

Related Questions