代码之家  ›  专栏  ›  技术社区  ›  Justin G

命名空间“System.Diagnostics”中不存在类型或命名空间名称“Process”

  •  0
  • Justin G  · 技术社区  · 7 年前

    这个错误对我来说毫无意义。 我正在使用CodeDOM编译一个可执行文件。

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

    以下是我编译源代码的方式:

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

    下面是我正在编译的源代码中出现错误的部分:

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

    1 回复  |  直到 7 年前
        1
  •  2
  •   Gusman    7 年前

    您没有将引用传递给 CompileCsharpSource

    改变 Compile

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