Monday, June 29, 2015

Dynamically Compile C# Code

Sample code to dynamically compile the C# source code and generate the Exe/DLL. You can write the code at runtime and execute or make a DLL.

Namespace :

using System.CodeDom.Compiler;




Code:

CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");
            string Output = "Out.exe";

         
            System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
            //Make sure we generate an EXE, not a DLL
            //parameters.GenerateExecutable = true; // set to false to generate DLL.
            parameters.OutputAssembly = Output;
            CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, txtSource.Text);

            if (results.Errors.Count > 0)
            {
                txtStatus.ForeColor = Color.Red;
                foreach (CompilerError CompErr in results.Errors)
                {
                    txtStatus.Text = txtStatus.Text +
                                "Line number " + CompErr.Line +
                                ", Error Number: " + CompErr.ErrorNumber +
                                ", '" + CompErr.ErrorText + ";" +
                                Environment.NewLine + Environment.NewLine;
                }
            }
            else
            {
                //Successful Compile
                txtStatus.ForeColor = Color.Blue;
                txtStatus.Text = "Success";
                //Launch our EXE
                Process.Start(Output);
            }

CSV to DataGridView

Sample code to show the content of the CSV (Comma Separated File) in DataGrid View with the filter & sorting in C#.

Namespace :

using System.Data.OleDb;


Code:

 OleDbConnection conn = new OleDbConnection
           ("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " +
             Path.GetDirectoryName(@"D:\TEST\test.txt") + 
             "; Extended Properties = 'Text;FMT=Delimited'");

            conn.Open();

            OleDbDataAdapter adapter = new OleDbDataAdapter
                   ("SELECT * FROM " + Path.GetFileName(@"D:\TEST\test.txt"), conn);

            DataSet ds = new DataSet("Temp");
            adapter.Fill(ds);

            conn.Close();

            dataGridView1.DataSource = ds.Tables[0] ;    


You can add the filter condition on dataset as below,
First create the Default view from the DatagridView datasource.

  string rowFilter = string.Format("column1 LIKE '%{0}%' AND column2 = 1",
                                  "test");
            (dataGridView1.DataSource as DataTable).DefaultView.RowFilter = rowFilter;

         

If you want to sort the DatagridView, set the sort column to the Default view.
.

   (dataGridView1.DataSource as DataTable).DefaultView.Sort = "column1";

Tuesday, June 16, 2015

Launching Application .EXE from C#

Sample code to Launch the application exe from c#.

Namespace :

using System.Diagnostics;


Code:


// Prepare the process to run
ProcessStartInfo start = new ProcessStartInfo();

// Enter in the command line arguments, everything you would enter after the executable name itself
start.Arguments = arguments; 

// Enter the executable to run, including the complete path
start.FileName = ExeName;

// Do you want to show a console window?
start.WindowStyle = ProcessWindowStyle.Hidden;
start.CreateNoWindow = true;
int exitCode; 


// Run the external process & wait for it to finish
using (Process proc = Process.Start(start))
{
     proc.WaitForExit();

     // Retrieve the app's exit code
     exitCode = proc.ExitCode;
}



Or


Simply write below,

System.Diagnostics.Process.Start( @"C:\Windows\System32\Notepad.exe" );