无法识别我的变量

无法识别我的变量

我正在尝试从exe文件加载代码,然后将其创建到新的.exe文件中。但这无法识别我的变量“ SourceCode”。它说名称“ SourceCode”在当前上下文中不存在

     private void button1_Click(object sender, EventArgs e)
    {

        using (FileStream SourceCode = new FileStream("thecode.exe", FileMode.Open, FileAccess.ReadWrite, FileShare.None));
        string Output = textBox3.Text;
        String[] Assembly = { "System.dll", "System.Drawing.dll", "System.Windows.Forms.dll" };
        CodeDomProvider CodeCompiler = CodeDomProvider.CreateProvider("CSharp");
        CompilerParameters Parameters = new CompilerParameters(Assembly, "");

        Parameters.OutputAssembly = Output;
        Parameters.GenerateExecutable = true;
        Parameters.GenerateInMemory = false;
        Parameters.WarningLevel = 3;
        Parameters.TreatWarningsAsErrors = true;
        Parameters.CompilerOptions = "/optimize+ /target:winexe";
        string Errors = null;
        try
        {
            CompilerResults Results = null;
            Results = CodeCompiler.CompileAssemblyFromSource(Parameters, SourceCode); //This here is giving me an error
            if (Results.Errors.Count > 0)
            {
                Errors = "";
                foreach (System.CodeDom.Compiler.CompilerError CompileError in Results.Errors)
                {
                    Errors += "Line number " + CompileError.Line + ", Error Number: " + CompileError.ErrorNumber + ", '" + CompileError.ErrorText + ";\r\n\r\n";
                }

最佳答案

下一行以分号';'结尾

using (FileStream SourceCode = new FileStream("thecode.exe", FileMode.Open, FileAccess.ReadWrite, FileShare.None));


在C#中,分号是语句终止符而不是行终止符。
您应该这样声明您的using语句

using(var Bar = new Foo())
{
}


从而使您的代码:

using (FileStream SourceCode = new FileStream("thecode.exe", FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
}

10-06 13:50