I'm working on a small project that involves compiling code. I keep getting this error: An unhandled exception of type System.ArgumentException
occurred in mscorlib.dll Illegal characters in path.
I've tried finding the source of the problem, this line of code seems to be the problem:
CompilerResults cr = provider.CompileAssemblyFromFile(parameters, source1);
This is my code for my class:
using System;
using System.IO;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
namespace Plugin___Prototype
{
class CompileCode
{
public void Compile()
{
string source1 = File.ReadAllText(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\test.cs");
//string source2 = File.ReadAllText(@"Source path here");
Console.WriteLine(source1);
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
parameters.ReferencedAssemblies.Add("System.dll");
parameters.GenerateExecutable = true;
parameters.OutputAssembly = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\app1.exe";
Console.WriteLine(parameters.OutputAssembly);
parameters.GenerateInMemory = false;
CompilerResults cr = provider.CompileAssemblyFromFile(parameters, source1);
if (cr.Errors.Count == 0)
Console.WriteLine("No Errors");
else
{
foreach (CompilerError error in cr.Errors)
Console.WriteLine(error.ErrorText);
}
Console.ReadLine();
}
}
}
This is the output:
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Illegal characters in path.
The program '[14128] Plugin - Prototype.exe' has exited with code -1 (0xffffffff).
My expected result is for app1.exe to generated in my documents folder.
EDIT: These are the contents of source1:
// A Hello World! program in C#.
using System;
namespace HelloWorld
{
class Hello
{
static void Main()
{
Console.WriteLine("Hello World!");
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
}
I've fixed it, CompilerResults cr = provider.CompileAssemblyFromFile(parameters, source1);
, wanted the file not the contents of the file.