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

Sunday, October 20, 2013

Selecting data from two Different SQL Servers

For selecting the data from two databases of two different servers, first we have to link the two servers.

sp_addlinkedserver('servername')

Then you can run the query like,

select * from Table1
unionall
select * from [server2].[database].[dbo].[Table1]

Read Text File using C#

Article about reading the Text file line by line using the StreamReader in C#


Namespace :

 using System.IO;

Code:

string line;

// Read the file and display it line by line.
StreamReader file = new StreamReader(@"c:\test.txt");
while((line = file.ReadLine()) != null)
{
Console.WriteLine (line);
counter++;
}

file.Close();

Friday, June 21, 2013

Get Serial Number of Hard Drive using C#

To get the Serial # of HDD using C#

Namespace :

Imports System.Management


Code:

Dim devid As String = ""
Dim driveser As String = ""

Try
      Dim searcher As New ManagementObjectSearcher( _
      "root\CIMV2", "SELECT * FROM Win32_DiskDrive")
     
       For Each queryObj As ManagementObject In searcher.Get()
       If queryObj("SerialNumber") <> "" Then driveser = queryObj("SerialNumber")
                Debug.Print(queryObj("Model") + ":" + driveser)
       Next

Catch err As ManagementException
       Debug.Print("An error occurred while querying for WMI data: " & err.Message)
End Try

Saturday, April 13, 2013

Accept Only Numeric Textbox

Simple code to accept only numeric in Textbox. You have to handle the key press event of textbox and have to check the key character is numeric or not.

Code:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
    {
        e.Handled = true;
    }  
}