1

I'm trying to load code from an exe file then create it into a new .exe file. But it's not recognizing my variable "SourceCode". It says the name "SourceCode" does not exist in the current context

     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";
                }
1
  • hope you got sorted out. Could you please mark on of the answers correct? Commented Aug 20, 2015 at 8:50

2 Answers 2

8

The following line is ending with a semicolon ';'

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

In C# the semicolon is a statement terminator rather than a line terminator. You should declare your using statements like this

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

Thus making your code:

using (FileStream SourceCode = new FileStream("thecode.exe", FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
}
Sign up to request clarification or add additional context in comments.

Comments

3

Your using statement at the top has a ; at the end. A using block is used to ensure a Disposable resource is disposed at the end of the block. In this case, without { }, your using statement is kind of useless. Either your expand the block of code using { }, or you just declare the variable without the using statement. Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.