Click here to Skip to main content
16,021,125 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have created an Editor. In That editor i am calling WriteLog Method, Which is hard coded in my program, with some string value. There is Button for With Compile Your Program Text. On that Click I am trying to save a file with name Variable.cs. Than From my code I am Calling C# Compiler at the same time to produce result.


This Is My Code to Save File
C#
private void SaveCSFile()
        {
            MyCSFile = string.Empty;
            //---------------------------Class Declaration-----------------------------//
            MyCSFile = @"using System;
            class Variable"
            + "{";
            MyCSFile += ConvertGrid(dataGridView1);
            MyCSFile += ConvertGrid(dataGridView2);
            MyCSFile += @"public Variable()" +
                "{";
                   MyCSFile+= T2.Text +
                "}";
            MyCSFile += @"public void WriteLog(String Msg)
                    {
                         Console.WriteLine(Msg);
                        Console.ReadLine();
                    }
                     public string toString(string msg)
                     {
                        return msg.ToString();
                     }
                    }";

            //-------------------------------------------------------------Declaring Main Method-----------------//
            MyCSFile += @"class MainClass
                          {";
             MyCSFile+=@"static void Main()" + "\n" +
                        "{";
             MyCSFile += @"Variable v=new Variable();";
            //MyCSFile += "v."+ConvertTab(tabControl3.SelectedTab.Text) + "\n";
             MyCSFile += "v."+T1.Text+ "\n";
            MyCSFile += "}}";
           MessageBox.Show(MyCSFile);
           //-----------------------------Writing CS File---------------------------//

           writer = System.IO.File.CreateText(FilePath);
           writer.WriteLine(MyCSFile.Trim()+System.Environment.NewLine);
           writer.Close();

        }



This The Code Where I am Calling C# Compiler And Passing the Source File
C#
private void GenerateScript()
        {
            SaveCSFile();
            //string AppName="demoFile.exe";
            string AppName = tabControl3.SelectedTab.Text + ".exe";
            
            //string mainClass="HelloWorld";
            string mainClass = tabControl3.SelectedTab.Text;

            CSharpCodeProvider code = new CSharpCodeProvider();
            ICodeCompiler compiler = code.CreateCompiler();
            CompilerParameters param = new CompilerParameters();
            //param.GenerateExecutable = true;
            param.GenerateInMemory = true;
            param.OutputAssembly = AppName.ToString();
            param.MainClass = mainClass.ToString();
            foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
            {
                param.ReferencedAssemblies.Add(asm.Location);                
            }
            string Mycode = MyCSFile ;
            //CompilerResults result = compiler.CompileAssemblyFromSource(param, Mycode);
            CompilerResults result = compiler.CompileAssemblyFromFile(param, "Variable.cs");
            if (result.Errors.Count > 0)
            {
                string error = "Compilation error :\n";
                foreach (CompilerError err in result.Errors)
                {
                    error += err.ErrorText+" "+"In Line Number :"+err.Line +" ";
                   
                }
                MessageBox.Show(this, error, "Error");                
            }
            else
            {
                #region Try to ExecuteApplication
                try
                {
                    if (!System.IO.File.Exists(AppName))
                    {
                        MessageBox.Show("FileName" + AppName + " Not Found");
                        return;
                    }
                    
                    ProcessStartInfo pinfo = new ProcessStartInfo
                    {
                         FileName = AppName,
                        Arguments = FilePath,
                        UseShellExecute=false
                    };
                    Process.Start(AppName, FilePath);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                #endregion
            }
        }


here I call WriteLog Method And Passing a string value like this
WriteLog("Hello World");
Form a notepad i created.. But i am getting error every time
"The specified executable is not a valid application for this OS platform"
pls help
Thank you.....
Posted
Updated 11-Jul-13 20:40pm
v3
Comments
Sergey Alexandrovich Kryukov 12-Jul-13 1:40am    
What are you trying to achieve? What's the ultimate goal? Why doing all that?

The problem can be anywhere. An external file could be anything: compiled for wrong instruction-set architecture, for example. You should explain everything, provide essential information, which may need some research of your side. A code dump cannot work just like that.

—SA
Anjanee Kumar Singh 12-Jul-13 1:45am    
I have created an Editor. In That editor i am calling WriteLog Method, Which is hard coded in my program, with some string value. There is Button for With Compile Your Program Text. On that Click I am trying to save a file with name Variable.cs.
Than From my code I am Calling C# Compiler at the same time to produce result. That's It.
Sergey Alexandrovich Kryukov 12-Jul-13 1:52am    
First of all, don't call C# compiler, use CodeDOM.
—SA
Anjanee Kumar Singh 12-Jul-13 1:55am    
Thank you Sir.
Let Me try .

Anjanee Kumar Singh 12-Jul-13 1:56am    
Is There Any Link so that i can take some help on CodeDOM

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900