C# 有没有一种方法可以像自动删除文件一样删除文件夹?

C# 有没有一种方法可以像自动删除文件一样删除文件夹?,c#,.net,directory,filesystems,delete-file,C#,.net,Directory,Filesystems,Delete File,使用我们可以在关闭最后一个句柄时删除文件本身,这对于在程序关闭时要删除的临时文件非常有用。我创建了以下函数 /// <summary> /// Create a file in the temp directory that will be automatically deleted when the program is closed /// </summary> /// <param name="filename">The name of the file

使用我们可以在关闭最后一个句柄时删除文件本身,这对于在程序关闭时要删除的临时文件非常有用。我创建了以下函数

/// <summary>
/// Create a file in the temp directory that will be automatically deleted when the program is closed
/// </summary>
/// <param name="filename">The name of the file</param>
/// <param name="file">The data to write out to the file</param>
/// <returns>A file stream that must be kept in scope or the file will be deleted.</returns>
private static FileStream CreateAutoDeleteFile(string filename, byte[] file)
{
    //get the GUID for this assembly.
    var attribute = (GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), true)[0];
    var assemblyGuid = attribute.Value;

    //Create the folder for the files to be saved in.
    string folder = Path.Combine(Path.GetTempPath(), assemblyGuid);
    Directory.CreateDirectory(folder);

    var fs = new FileStream(Path.Combine(folder, filename), FileMode.OpenOrCreate, FileAccess.ReadWrite,
                                   FileShare.ReadWrite, 16 << 10, //16k buffer
                                   FileOptions.DeleteOnClose);

    //Check and see if the file has already been created, if not write it out.
    if (fs.Length == 0)
    {
        fs.Write(file, 0, file.Length);
        fs.Flush();
    }

    return fs;
}
//
///在临时目录中创建一个文件,该文件将在程序关闭时自动删除
/// 
///文件名
///要写入文件的数据
///必须保留在作用域中的文件流,否则将删除该文件。
私有静态文件流CreateAutoDeleteFile(字符串文件名,字节[]文件)
{
//获取此程序集的GUID。
var attribute=(GuidAttribute)Assembly.getExecutionGassembly().GetCustomAttributes(typeof(GuidAttribute),true)[0];
var assemblyGuid=attribute.Value;
//为要保存的文件创建文件夹。
string folder=Path.Combine(Path.GetTempPath(),assemblyGuid);
创建目录(文件夹);
var fs=new FileStream(Path.Combine(文件夹,文件名),FileMode.OpenOrCreate,FileAccess.ReadWrite,
FileShare.ReadWrite,16 NTFS(或在Windows FS API中公开或在.NET中直接支持)不知道“清空目录时删除目录”的概念。我想可以使用。。