1

My program often compile c#-code and sometimes i got a ArgumentException like "The file name 'C:\Users--\AppData\Local\Temp\wvpc3m5m.0.cs' was already in the collection. Parameter name: fileName".

Settings of compiler is next:

public void Init()
{
    this.compilerParameters = new CompilerParameters
    {
        GenerateExecutable = false,
        GenerateInMemory = true
    };
}

Compilation:

public CompilerResults Compile(String code)
{
    CompilerResults result = this.codeProvider
        .CompileAssemblyFromSource(this.compilerParameters, code);

    return result;
}

I think that the codeProvider write passed string into a file, and sometimes he try to write in one and the same file twice.

2
  • There is no point in keeping the CodeProvider around. Just create a fresh one with the new operator. Commented Mar 25, 2014 at 10:30
  • I do this, but exception keep throwing Commented Mar 25, 2014 at 10:37

1 Answer 1

1

The problem was that all CSharpCodeProvider's instance can compile the code once a time slot. This code solve problem:

    private readonly Object lockCompile = new Object();
    public CompilerResults Compile(String code)
    {
        CompilerResults result = null;
        lock (lockCompile)
        {
            using (CSharpCodeProvider codeProvider = new CSharpCodeProvider())
            {
                result = codeProvider
                   .CompileAssemblyFromSource(this.compilerParameters, code);
            }
        }

        return result;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.