Search code examples
c#.netcodedomsystem.diagnostics

The type or namespace name 'Process' does not exist in the namespace 'System.Diagnostics'


This error makes absolutely no sense to me. I'm using CodeDOM to compile an executable. Here's my class for compiling:

using System;
using System.CodeDom.Compiler;
using System.IO;
using Microsoft.CSharp;

class Compiler
{
    public static bool Compile(string[] sources, string output, params 
string[] references)
    {
        var results = CompileCsharpSource(sources, "result.exe");
        if (results.Errors.Count == 0)
            return true;
        else
    {
        foreach (CompilerError error in results.Errors)
            Console.WriteLine(error.Line + ": " + error.ErrorText);
    }
    return false;
}

    private static CompilerResults CompileCsharpSource(string[] sources, 
string output, params string[] references)
    {
        var parameters = new CompilerParameters(references, output);
        parameters.GenerateExecutable = true;
        using (var provider = new CSharpCodeProvider())
            return provider.CompileAssemblyFromSource(parameters, sources);
    }
}

Here's how I'm compiling my source:

Compiler.Compile(srcList, "test.exe", new string[] { "System.dll", "System.Core.dll", "mscorlib.dll" });

And here's the part of the source code I'm compiling where the error occurs:

System.Diagnostics.Process p;
if (System.Diagnostics.Process.GetProcessesByName("whatever").Length > 0) 
  p = System.Diagnostics.Process.GetProcessesByName("whatever")[0]; 
else 
  return false;

So I'm referencing System.dll when compiling, and I'm writing System.Diagnostics in front of process, (I tried using System.Diagnostics too but I produced a similar and less specific error), and for some reason I'm getting this error. I'd appreciate some help.


Solution

  • You aren't passing the references to CompileCsharpSource.

    Change Compile to this:

    public static bool Compile(string[] sources, string output, params string[] references)
    {
        var results = CompileCsharpSource(sources, "result.exe", references);
        if (results.Errors.Count == 0)
                return true;
        else
        {
            foreach (CompilerError error in results.Errors)
                Console.WriteLine(error.Line + ": " + error.ErrorText);
        }
        return false;
    }