Friday, July 3, 2015

Launch Application exe with Javascript.

Sample code to launch the application exe from the Javascript.

Code:

     var ws = new ActiveXObject("WScript.Shell");

     ws.Exec("C:\\TestApp.exe");

Read & Write a File with JavaScript

Sample code to do the File manipulation in JavaScript.
This code illustrate, how to Create, Read & Write the text in file using the Java Script.

Code:

           // Reading text file.

           var fso  = new ActiveXObject("Scripting.FileSystemObject");
           var fhRead = fso.OpenTextFile("C:\\Test.txt",1);
           var Fltext = fhRead.ReadAll();
           fhRead.Close();
 

            // Creating text file.

            var fh = fso.CreateTextFile("C:\\Test.txt", true);
            fh.WriteLine("Sample");
            fh.Close();
         
                 
        

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" );

Saturday, January 10, 2015

Delete Files & Folders

Sample code to delete the Files and the Directories.

Namespace :


using System.IO;


Code:


Delete Directory:
         
            Directory.Delete("C:\test\")

            If Directory have the sub directories then,


            Directory.Delete("C:\test\", True)


Delete Files:


           File.Delete("C:\test\text.txt")


           To delete multiple files from Directory,


           string[] Files= Directory.GetFiles(@"c:\test\");


           foreach (string filePath in Files)
                  File.Delete(filePath);

Get Files from Directory using C#

Sample code to get the list of files from the source Directory. You can also add filter to search the specific file type only.

Namespace :
using System.IO;


Code:

Directory.GetFiles returns string array with files names.

string[] files = Directory.GetFiles(@"c:\Test\");


You can also search the files with specific pattern.

string[] files = Directory.GetFiles(@"c:\Test\", "*.txt");


To get the list of files from directory including the sub directories,

string[] filePaths = Directory.GetFiles(@"c:\Test\", "*.txt",SearchOption.AllDirectories);