CompileAssemblyFromDom throws access denied except

2019-04-11 02:01发布

问题:

The code:

using (var codeProvider = new CSharpCodeProvider())
{
    var compilerParameter = new CompilerParameters(
                _assemblies, assemblyName, false)
            {
                GenerateInMemory = true,
                CompilerOptions = "/optimize"
            };

    var compilerResults = codeProvider.CompileAssemblyFromDom(
                compilerParameter, templateResults.Select(r => r.GeneratedCode)
                .ToArray());
}

It throws exception: CS1567 Error generating Win32 resource: Access is denied.

What does it mean?

回答1:

I get this working. The problem was that assemblyName was file name not path, so I changed

assemblyName = Path.Combine(Path.GetTempPath(), assemblyName);

And it start working!



回答2:

I got same error, but AssemblyName didn't fix it for me. I was able to fix it by using TempFiles parameter. Also, for some reason it does not work in the temp path itself, only works when I create my own subfolder there.

string tempPath = Path.GetTempPath() + "\\mysubfolder";
Directory.CreateDirectory(tempPath);

var parameters = new CompilerParameters(includeAssemblies.ToArray())
                {                    
                    GenerateInMemory = true,
                    TempFiles = new TempFileCollection(tempPath)
                };