Sunday, December 13, 2015

Get Geo Location with Google Map

Sample code to show how to use the Google Map API for Geo location details.
Code to get your current location (Latitude & Longitude) and how to add the marker on the Google Map.

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function (p) {
        var LatLng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude);
        var mapOptions = {
            center: LatLng,
            zoom: 13,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
        var marker = new google.maps.Marker({
            position: LatLng,
            map: map,
            title: "Your location: Latitude: " + p.coords.latitude + "Longitude: " + p.coords.longitude
        });

        google.maps.event.addListener(marker, "click", function (e) {
            var infoWindow = new google.maps.InfoWindow();
            infoWindow.setContent(marker.title);
            infoWindow.open(map, marker);
        });
    });

else 
{
    alert('Geo Location feature is not supported in this browser.');
}

Sunday, November 29, 2015

OCR with Microsoft Office Document Imaging

This article will show how to integrate the Office 2007 OCR engine. This will perform the OCR on the scanned image and convert it to the text.

It's necessary that you have installed the Microsoft Office Document Imaging 12.0 Type Library. MS office setup doesn't install this component by default, being necessary to install it later. To do this:
  • Run the Office 2007 installation setup
  • Click on the button Add or Remove Features
  • Make sure that the component is installed

Add reference of Microsoft Office Document Imaging DLL in project by clicking on Add Reference from solution explorer. At the COM tab, select Microsoft Office Document Imaging 12.0 Type Library.

Code:

            MODI.Document md = new MODI.Document(); 
            md.Create("FileName.TIF"); 
            md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true); 
            MODI.Image image = (MODI.Image)md.Images[0];

            MODI.Layout layout = image.Layout;
           
            //create text file with the same Image file name 
            FileStream createFile = new FileStream("fileName.txt",FileMode.CreateNew);

            //save the image text in the text file 
            StreamWriter writeFile = new StreamWriter(createFile);
            writeFile.Write(image.Layout.Text); 

            writeFile.Close(); 


if you want to read text word by word then use below,

         for (int i = 0; i < layout.Words.Count; i++)
                    {
                        MODI.Word word = (MODI.Word)layout.Words[i];
                        string strText = word.Text;

                    }

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