CompileAssemblyFromDom throws access denied exception

不问归期 提交于 2019-12-06 08:02:33

问题


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


来源:https://stackoverflow.com/questions/16816449/compileassemblyfromdom-throws-access-denied-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!