Delete files after they are created

so I create dll files, run them, and then I want to delete them.

However, when I try to delete them, I get an exception because it claims that it is still being used by another process.

I assume that the code used to create the files does not delete resources correctly to allow file deletion after, here is my code to create the file.

if (!Directory.Exists(PathToRobots + Generation))
{
    Directory.CreateDirectory(PathToRobots + Generation);
}

File.WriteAllText(Path.Combine(PathToRobots + Generation, NameSpace + GetRobotName() + robotNumber + ".cs"), code);


CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters()
{
    GenerateInMemory = false,
    GenerateExecutable = false, // True = EXE, False = DLL
    IncludeDebugInformation = true,
    OutputAssembly = Path.Combine(FileName + ".dll") // Compilation name
};

parameters.ReferencedAssemblies.Add(@"robocode.dll");

CompilerResults results = provider.CompileAssemblyFromSource(parameters, code);

if (results.Errors.HasErrors)
{
    StringBuilder sb = new StringBuilder();

    foreach (CompilerError error in results.Errors)
    {
        sb.AppendLine(String.Format("Error ({0}): {1}", error.ErrorNumber, error.ErrorText));
    }

    throw new InvalidOperationException(sb.ToString());
}

Assembly assembly = results.CompiledAssembly;

provider.Dispose();

The code to delete the file is quite simple and looks like this:

var files = Directory.GetFiles(DirectoryPath);
foreach (var file in files)
{
    File.Delete(file);
}

Any idea as to why I cannot delete the files?

+4
source share
1 answer

See notes in CompilerResults.CompiledAssembly Property

CompiledAssembly Load . get accessor, , AppDomain .

, :

Assembly assembly = results.CompiledAssembly;

, , . , ( ).

0

Source: https://habr.com/ru/post/1629425/


All Articles