Showing posts with label My Computer. Show all posts
Showing posts with label My Computer. Show all posts

5 November 2014

Show delimiter Text file data to DataGridView

Hi All,

Lets see how to show the delimiter text file in DataGridView in C#.  Lot of use face the problem when we have CSV or any other delimiter text file and asked to show it as table in C# DataGridView.

As one user asked at Stackoverflow


You can have to copy paste the following code and ready to go..

using System.Data;
using System.IO;


public class Helper
{

 public static DataTable DataTableFromTextFile(string location, char delimiter=',')
 {
     DataTable result;

     string[] LineArray = File.ReadAllLines(location);

     result = FormDataTable(LineArray, delimiter);

     return result;
 }


 private static DataTable FormDataTable(string []LineArray, char delimiter)
 {
     bool IsHeaderSet = false;

     DataTable dt = new DataTable();

     AddColumnToTable(LineArray, delimiter, ref dt);

     AddRowToTable(LineArray, delimiter, ref dt);

     return dt;
 }


 private static void AddRowToTable(string []valueCollection, char delimiter, ref DataTable dt)
 {

     for (int i = 1; i < valueCollection.Length; i++)
     {
     string[] values = valueCollection[i].Split(delimiter);

     DataRow dr = dt.NewRow();

     for (int j = 0; j < values.Length; j++)
     {
         dr[j] = values[j];
     }

     dt.Rows.Add(dr);
     }
 }


 private static void AddColumnToTable(string []columnCollection, char delimiter, ref DataTable dt)
 {
     string[] columns = columnCollection[0].Split(delimiter);

     foreach (string columnName in columns)
     {
     DataColumn dc = new DataColumn(columnName, typeof(string));
     dt.Columns.Add(dc);
     }

 }

}

Thats all.. ready to use.. Now let me tell you how to bind this with DataGridView just call

// if | is delimiter char
//fname| sname| age
//deepak| sharma| 23
//Gaurav| sharma| 32
//Alok| Kumar| 33
dataGridView1.DataSource = Helper.DataTableFromTextFile("Testing.txt", '|');


//if comma , is delimiter char like - 
//fname, sname, age
//deepak, sharma, 23
//Gaurav, sharma, 32
//Alok, Kumar, 33
dataGridView1.DataSource = Helper.DataTableFromTextFile("Testing.txt");


if pipeline(|) is delimiter character else pass what else it is.  Default delimiter char is common(,) so in case of (,) no need to even pass it.

It works like charm. :)




Happy Coding.. and Sharing.. :)

16 January 2014

Pipe (|) symbol, Blank line and Comment in batch programming

Hi Friends,

Lets see how can you print the pipe line ( | ) symbol in batch file.  If you directly use the | symbol then batch file will throw an error.  To display the pipeline symbol in batch file, you have to use the "^" symbol just before the pipeline symbol.

As we know we use "echo hello" to print the hello then you might think to print line we can use the "echo" only. Am I right? but to display the blank line, we have to use the dot( . ) just after the echo word "echo." remember there should not be any space between echo and dot(.)

To mark the line as comment we have to use the double colon ( :: )

 Check the below example -

@echo off
echo.
echo above line is blank
::echo This line is comment as the line starts with Double colon (::)
echo +--------------------------------+
echo ^|                          ^|
echo ^|        Hello World..!!      ^|
echo ^|                          ^|
echo +--------------------------------+
pause

when you will try to run the above batch code, you will get the below output.


Happy Sharing.. :)

Command Line Argument to Batch file (Parameter)

Hi All,

Lets see how can we pass the parameter to batch file.  If you have worked with C, C++ and anyother programming language then I hope you must be familir with "Command Line Args" and whats the use of it.

lets see the basic batch file code to print the value.
@echo off
set n=Hello World..!!!
echo Hello Friends the value stored in variable n is = %n%
pause

when i will run the above batch code it will show the output as below -

Hello Friends the value stored in variable n is = Hello World..!!!
Press any key to continue . . .

but I want to pass the value to the batch code, to run the file I have to use the file name followed by the value we want to pass.

batch_file.bat value1 value2

now our job is to take these value from the command line and use it in our batch code. %1 will refer to the first parameter value and %2 will point to the second value.

@echo off

if %1.==. goto novar1
if %2.==. goto novar2

set first_var= %1
set second_var=%2

echo first variable pass as command line is = %first_var%
echo second variable pass as command line is = %second_var%
goto end

:novar1
echo no command line argument passed in.
goto end

:novar2
echo only one command line argument passed in.
goto end

:end
pause
so in this way you can pass the command line argument to batch file,  and can check either command line argument is passed or not.

Happy Coding & Sharing..

2 January 2014

This app needs permission to use the camera. Camera app: error in Windows 8.

Hi All,

Lets see how to enable the Camera App's in window 8 once it got disable by mistake.  First time when you open the "Camera app" in "Windows 8" it will prompt and ask you to for the permission to access the hardware - "Camera and Mic".  If by mistake you click on "Block" that mean windows will consider this app as blocked app which can not access the "Camera and Mic".  Now whenever you will start this app you will get a message



Now lets see you to allow the this app to access your "Camera and Mic"

1.  Open your Windows 8 "Camera App".  You will see the above error screen.

2. Press "Win + C" to bring the Windows 8 Charms Bar.

3. Click on the "Settings".

4.  Select the "Permissions" from the setting Menu.

5.  Change the access permission of your "Camera and Mic".

6.  Done. Close the Camera app and reopen it.  Its work. :)

Happy knowledge sharing.. !! :)
Happy Blogging.. :)

18 December 2013

Detect the Removable drive in C#

Hi Friends,

Let see how can you get the USB drive information using the C# code.  I mean the code which shows us if the Removable drive is inserted in computer.

If you want to trigger some program, you have to implement this code in window service and check for the condition in loop.

For the console program the below code shows you how can you get the removable drive and get the other information related to that drive.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DetectRemovableDrive
{
  class Program
  {
    static void Main(string[] args)
    {
      // for all the ready drives
      IEnumerable a= DriveInfo.GetDrives().Where(d => d.IsReady);

      // if you want to get only Removable drives use the below two lines..
      //IEnumerable a = DriveInfo.GetDrives().Where(d => d.IsReady 
      //&& d.DriveType == System.IO.DriveType.Removable);

      Console.WriteLine("Total {0} ready drives found.!!\n", a.Count());

      foreach(DriveInfo temp in a)
      {
        Console.WriteLine("\tDrive {0} has the label \"{1}\"",
                  temp.Name,
                  temp.VolumeLabel==""?"Local Disk":temp.VolumeLabel);

        Console.WriteLine("\t\tTotal size {0} bytes, free is {1} bytes.\n",
                  temp.TotalSize,
                  temp.TotalFreeSpace);
      }
      Console.ReadKey();
    }
  }
}
Create a console program the paste the following code in Program.cs file. And compile the code.

You will see the code like below

Total 5 ready drives found.!!

 Drive C:\ has the label "Local Disk"
  Total size 104752738304 bytes, free is 27952164864 bytes.

 Drive D:\ has the label "Local Disk"
  Total size 364670611456 bytes, free is 207251906560 bytes.

 Drive G:\ has the label "Deepak"
  Total size 214748360704 bytes, free is 214185193472 bytes.

 Drive H:\ has the label "WD Unlocker"
  Total size 7507968 bytes, free is 0 bytes.

 Drive I:\ has the label "My Passport"
  Total size 1000169533440 bytes, free is 836563316736 bytes.



  Remember- d.IsReady is must, other wise it will throw error when it will try to get the information of the CD-Rom and found CD-Rom is empty mean we have not inserted any CD or DVD, in this situation CD-Rom is not ready so by applying the proper LINQ we have removed the not ready drive from the all drive collection.

Happy Reading and knowledge sharing.. !! :)

17 December 2013

Create your own Right Click Menu

Hi Friends,

Sometime we want to create the right click menu as shortcut so that we can easy our task.  Sometime when we install any application it create a right click shortcut.

I have created a tool for you.  You just need to download it and run it. Let me explain about the application.

It has 4 options-

  • File-  When we want to create the right click menu on a file.  If you add menu by choosing this option it will be shown whenever you right click on any file.

  • Folder-  When we want to create the right click menu on a Folder.  Just create the menu by choosing this and right click any folder, you can find your newly created menu over there.

  • Desktop Background- When you create a menu by choosing this option.  You can find newly created menu when you right click on your desktop. (Note-  Remember because desktop is also a Directory that's why it is also showing the menu created by choosing 4th Option)

  • Directory Background- This is same as the Desktop Background menu, the only difference is that you can find this newly menu whenever you right click on empty space in any directory including your desktop.

You can download the executable file from the following link.
Download from here

Note-  Remember You must have installed the .Net framework (4.0, 3.5, 3.0 or 2).  As its a .net application so for the execution it needs the environment, provided by the .Net framework.

Happy Tricks and Coding.

10 December 2013

Change Screen Resolution using C#

Hi All,

I'm back.  Today let see how can we change the screen resolution using the C#. You might aware we can get the screen resolution in C# using the Screen class.

Screen Srn=Screen.PrimaryScreen;
            
int tempWidth=Srn.Bounds.Width;
int tempHeight=Srn.Bounds.Height;
Console.WriteLine("Current Screen width is {0} and height is {1}.", tempWidth, tempHeight);

and we get the output -
 Current Screen width is 1152 and height is 864.

Using the Screen class, we can get the screen resolution and size, but whats about to set the screen resolution as per the requirement.

Sometime we code in such a way we need a fix screen resolution mean to work the piece of code of whole snippet we require a size (x*y).

Using the Screen class we can verify either we have suitable environment or not but to set the environment to make the piece of code work we explicitly need to change the resolution of the screen.

I have code to change the resolution of the screen, you just need to down the DLL and you are ready to using it.

Download from here Download DLL Now.

After the downloading you are ready to go by including the dll in your project reference and you need to one line only

 MyTactics.blogspot.com.NewResolution n = new MyTactics.blogspot.com.NewResolution(1024, 768);



You can download the full sample from here. Download Full Sample.

First button to change the resolution to 1024 * 768, while the second button show the current resolution is 1152 * 864.  After clicking the First button resolution would be 1024 * 768 and we can revert back to original resolution by just clicking the Second Button.

Happy Coding and Knowledge sharing..!!!

19 September 2013

Life time IDM (Internet Download Manager)

Hello Friends,
Lets crack the IDM.  IDM is the tool generally we all are aware with. Its Internet Download Manager used to download the file with higher speed from internet. You can download the trial version from its official site click here. To continue use the IDM you have to register or purchase its license from Internet download manager site. It will cost you around $30.00 bucks, but that is again not for life time its for limited period of time. IDM is really a nice tool for downlaod purchase. But purchasing IDM is against Rule and definitely you also don't want to purchase it if you can get it free of cost. :)



You need to download the IDM Life Time Tool and Internet Download Manager(IDM), if you downloaded or installed. You can download these two from below links. -

IDM version 6.17 from here
IDM Life Time tool from here   - you need to extract it as its rar file use the password "MyTacticks" I will suggest you to disable your Antivirus while applying patch, as few antivirus may detect it as infected file. Its because this tool extract the information of IDM to apply the patch.

Steps -
1- Download and install the IDM. (You can download from above link)
2- Download Life time tool from above link.
3- Disable you AV for a while till you are applying the patch.
4- Extract life time tool.  Use the password  MyTacTics to extraction.
5- Run the file "IDMCrackForLife.exe" it will extract the few information about the IDM from you system.


6. In your system, you will get "Trial" at the place of "Full".
7. Now you just need to run it by clicking the button "Start".
8. After few seconds you will get a message that registration successful.
9. Open you IDM and see its registered with the name "Deepak.Sharma" in my case.  Or the user name you loggedin in your system.  You can change the name by unchecking the "Auto" checkbox.


10- See the registration tab is disabled thats mean IDM is already registered.

Happy Cracking.. :)


20 August 2013

How to set Icon of Drive

Hi Friends,

Sometime we want to set the icon of drive in my computer or any other removable device.  Today we will see how to set the icon of drives (HD partitions or removable devices)

open notepad and paste the following code

     [autorun]
     icon = .\media.ico

here media.ico is the icon file I want to set to the drive.  '.\media.ico' it shows media.ico are at same place.  Now save file file to the drive in which you want to set the icon as 'autorun.inf' extension.  Just consider the case if you want to set the icon to 'D:\' drive then save this file to 'D:\' drive.  "D:\autorun.inf". Now save the that icon file in 'D:\' drive as 'media.ico' file name we specified in autorun.inf file.


if the drive in which you set the icon file is removable remove it and reinsert it, it will show you the icon.  But if the drive is HD partition you have to restart the system. And you will see the icon at the place of default drive icon.

See the icon of 'I:\' drive (Pendrive).



Happy Tricks.. :)