Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 3rd, 2006, 04:36 AM
Registered User
 
Join Date: Oct 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default error occurred when programmatically compile

Hi,
An error occurred when programmatically compile another project from my present project.

The source code


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Diagnostics;
using System.IO;
using System.Globalization;

namespace Compile2
{


    class Class2
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>

        public bool CompileExecutable(String sourceName)
        {
            FileInfo sourceFile = new FileInfo(sourceName);
            //CodeDomProvider provider = null;
            CSharpCodeProvider provider = new CSharpCodeProvider();
            ICodeCompiler compiler = provider.CreateCompiler();
            bool compileOk = false;
            // provider = new Microsoft.CSharp.CSharpCodeProvider();


            if (provider != null)
            {

                // Format the executable file name.
                // Build the output assembly path using the current directory
                // and <source>_cs.exe or <source>_vb.exe.

                String exeName = String.Format(@"{0}\{1}.exe",
                    System.Environment.CurrentDirectory,
                    sourceFile.Name.Replace(".", "_"));


                string[] Myref ={ "System.dll","System.Drawing.dll","System.Windows. Forms.dll","System.Data.dll","System.Drawing.dll" };
                string MyAsm = "MyAsm.dll";

                CompilerParameters cp = new CompilerParameters(Myref, MyAsm);
               // CompilerParameters cp = new CompilerParameters();

                // Generate an executable instead of
                // a class library.
                cp.GenerateExecutable = true;

                // Specify the assembly file name to generate.
                cp.OutputAssembly = exeName;

                // Save the assembly as a physical file.
                cp.GenerateInMemory = false;

                // Set whether to treat all warnings as errors.
                cp.TreatWarningsAsErrors = false;

                // Invoke compilation of the source file.
                CompilerResults cr = compiler.CompileAssemblyFromFile(cp, sourceName);

                //CompilerResults cr = new CompilerResults();


                if (cr.Errors.Count > 0)
                {
                    // Display compilation errors.
                    Console.WriteLine("Errors building {0} into {1}",
                        sourceName, cr.PathToAssembly);
                    foreach (CompilerError ce in cr.Errors)
                    {
                        Console.WriteLine(" {0}", ce.ToString());
                        Console.WriteLine();
                    }
                }
                else
                {
                    // Display a successful compilation message.
                    Console.WriteLine("Source {0} built into {1} successfully.",
                        sourceName, cr.PathToAssembly);
                }

                // Return the results of the compilation.
                if (cr.Errors.Count > 0)
                {
                    compileOk = false;
                }
                else
                {
                    compileOk = true;
                }
            }
            return compileOk;
        }

    }







    class Program
    {
        static void Main(string[] args)
        {
            Class2 c2 = new Class2();
            c2.CompileExecutable("C:\\temp3\\temp3\\Program.cs ");
            Console.WriteLine("hai");
            Console.Read();
        }
    }
}

expecting quick response
thanks
ramesh







Similar Threads
Thread Thread Starter Forum Replies Last Post
error occurred... craft Dreamweaver (all versions) 1 November 1st, 2008 04:31 AM
VS2005 ReportViewer "An error occurred during..." VerbatimBOT ASP.NET 2.0 Professional 0 September 30th, 2008 06:39 AM
unexpected error has occurred tworkman BOOK: ASP.NET Website Programming Problem-Design-Solution 20 July 1st, 2007 09:49 PM
error:Exception occurred hastikeyvan Classic ASP Professional 1 September 13th, 2006 04:40 AM
element is not declared. An error occurred... dazzled XML 3 September 13th, 2004 04:55 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.