Abdalla
Abdalla

Reputation: 2073

Runtime C# Compile All Files in a Folder from Code

I am successfully doing run-time compilation using the code below. Except that when the number of input *.cs files is large I get the following error.

System.SystemException: Error running mono.exe: The filename or extension is too long.

I believe this is due to the command line length limit as in this article.

Here is the code

var codeProvider = new CSharpCodeProvider (
                   new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });

string EngineOutput = Path.GetFileName(dir) + ".dll";

parameters.GenerateExecutable = false;
parameters.OutputAssembly = EngineOutput;

CompilerResults results = codeProvider.CompileAssemblyFromFile (parameters, engineFiles.ToArray ());

Since all source files are in one folder, if there is a way to pass that folder using the -recurse programmatically will be great. Or if there were a method that takes a folder like codeProvider.CompileAssemblyFromDirectoryRecursive that would have also been great, but to my knowledge that doesn't exist.

I tried the following, but it didn't work. The compiler only picks the fake.cs and not the folder specified by /recurse.

parameters.CompilerOptions += " /recurse:" + dir + System.IO.Path.PathSeparator + "*.cs";
results = codeProvider.CompileAssemblyFromFile (parameters, new string[1] { "fake.cs" });

Thanks for advance.

Upvotes: 1

Views: 1276

Answers (1)

Abdalla
Abdalla

Reputation: 2073

I just replaced the PathSeparator with DirectorySeparator, then it works.

parameters.CompilerOptions += " /recurse:" + dir + System.IO.Path.DirectorySeparatorChar + "*.cs";

Upvotes: 1

Related Questions