How To Empty Recycle Bin Using C#


hey everyone , we going to learn how to empty the recycle bin using c# this pretty an easy way out . you just need to a button or lets say click event or anything that can do something now you just have to add this codes into your form1 code , i will put whole of my codes here so i am using a button to do the work

here is the codes


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

//Author : Mohamed Shimran
//Blog : http://www.ultimateprogrammingtutorials.blogspot.com

namespace RecycleBin
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        enum RecycleFlags : uint
        {
            SHERB_NOCONFIRMATION = 0x00000001,
            SHERB_NOPROGRESSUI = 0x00000001,
            SHERB_NOSOUND = 0x00000004
        }

        [DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
        static extern uint SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath,
        RecycleFlags dwFlags);
               private void button1_Click(object sender, EventArgs e)
        {
            uint result = SHEmptyRecycleBin(IntPtr.Zero, null, 0);
        }
    }
}
enjoy

How To Make A Sound Recorder In C#

sound recorder

hey ,today i will let you know how to create a sound recorder in c# by the way this is just a simple tutorial but this will let you know about the recording so you can make a  advanced recording application ok now first create a new project and name it and you need three buttons and one label so drag and and drop them from tool box into your form and now change the text of the each button as below

Button 1 - Record
Button2 - Stop And Save
Button3 - Play

now just keep the label 1 text empty now just double click your form and erase everything and add the whole code there

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

//Author : Mohamed Shimran
//Blog : http://www.ultimateprogrammingtutorials.blogspot.com

namespace soundrecorder
{
    public partial class Form1 : Form
    {
        [DllImport("winmm.dll")]
        private static extern int mciSendString(string MciComando, string MciRetorno, int MciRetornoLeng, int CallBack);

        string musica = "";
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Visible = true;
            mciSendString("open new type waveaudio alias Som", null, 0, 0);
            mciSendString("record Som", null, 0, 0);
        
        }

        private void button2_Click(object sender, EventArgs e)
        {
            label1.Visible = false;
            mciSendString("pause Som", null, 0, 0);

          

            SaveFileDialog save = new SaveFileDialog();

            save.Filter = "wave|*.wav";


            if (save.ShowDialog() == DialogResult.OK)
            {

                mciSendString("save Som " + save.FileName, null, 0, 0);
                mciSendString("close Som", null, 0, 0);
            }
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (musica == "")
            {
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "Wave|*.wav";
                if (open.ShowDialog() == DialogResult.OK) { musica = open.FileName; }
            }
            mciSendString("play " + musica, null, 0, 0);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Visible = false;
        }


    }
}


now debug and enjoy .

How To Draw Border At PictureBox Border In C#

s

hello i am up with this but this is something very small but very useful ok now what this is about , theres are 2button and a openfiledialog and when you click open the openfiledialog will be up so you just can go ahead and select a image and you click the draw button the picturebox out border will be green color .

How To Create A Folder Using C#

hello this is a simple tutorial not actually a tutorial but yah lets begin this is a console application so you dont need any forms you know that ok so create a new console application project and name it now when the coding part is loaded now you just need to understand the code first of all this is the whole code and this is a picture of the application


How To Get SQL Instance Using C#

hello guys this a simple tutorial that will give you the list of SQL servers available on your lan . ok lets begin making this just open c# and make a new project and name it whatever and after form finished loading add a button and change the text  to refresh and add combobox and at last add a label to make the ui beautiful now  add a class and name it getsql.cs
how to get sql instance using c# - adding class getsql

ok now you will see the class in the side bar ok now go to getsql.cs and add this codes

using System;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

//Author : Mohamed Shimran
//Blog : http://www.ultimateprogrammingtutorials.blogspot.com

namespace DBGrep
{
 public class SqlLocator
 {
  [DllImport("odbc32.dll")]
  private static extern short SQLAllocHandle(short hType, IntPtr inputHandle, out IntPtr outputHandle);
  [DllImport("odbc32.dll")]
  private static extern short SQLSetEnvAttr(IntPtr henv, int attribute, IntPtr valuePtr, int strLength);
  [DllImport("odbc32.dll")]
  private static extern short SQLFreeHandle(short hType, IntPtr handle); 
  [DllImport("odbc32.dll",CharSet=CharSet.Ansi)]
  private static extern short SQLBrowseConnect(IntPtr hconn, StringBuilder inString, 
   short inStringLength, StringBuilder outString, short outStringLength,
   out short outLengthNeeded);

  private const short SQL_HANDLE_ENV = 1;
  private const short SQL_HANDLE_DBC = 2;
  private const int SQL_ATTR_ODBC_VERSION = 200;
  private const int SQL_OV_ODBC3 = 3;
  private const short SQL_SUCCESS = 0;
  
  private const short SQL_NEED_DATA = 99;
  private const short DEFAULT_RESULT_SIZE = 1024;
  private const string SQL_DRIVER_STR = "DRIVER=SQL SERVER";
 
  private SqlLocator(){}

  public static string[] GetServers()
  {
   string[] retval = null;
   string txt = string.Empty;
   IntPtr henv = IntPtr.Zero;
   IntPtr hconn = IntPtr.Zero;
   StringBuilder inString = new StringBuilder(SQL_DRIVER_STR);
   StringBuilder outString = new StringBuilder(DEFAULT_RESULT_SIZE);
   short inStringLength = (short) inString.Length;
   short lenNeeded = 0;

   try
   {
    if (SQL_SUCCESS == SQLAllocHandle(SQL_HANDLE_ENV, henv, out henv))
    {
     if (SQL_SUCCESS == SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(IntPtr)SQL_OV_ODBC3,0))
     {
      if (SQL_SUCCESS == SQLAllocHandle(SQL_HANDLE_DBC, henv, out hconn))
      {
       if (SQL_NEED_DATA ==  SQLBrowseConnect(hconn, inString, inStringLength, outString, 
        DEFAULT_RESULT_SIZE, out lenNeeded))
       {
        if (DEFAULT_RESULT_SIZE < lenNeeded)
        {
         outString.Capacity = lenNeeded;
         if (SQL_NEED_DATA != SQLBrowseConnect(hconn, inString, inStringLength, outString, 
          lenNeeded,out lenNeeded))
         {
          throw new ApplicationException("Unabled to aquire SQL Servers from ODBC driver.");
         } 
        }
        txt = outString.ToString();
        int start = txt.IndexOf("{") + 1;
        int len = txt.IndexOf("}") - start;
        if ((start > 0) && (len > 0))
        {
         txt = txt.Substring(start,len);
        }
        else
        {
         txt = string.Empty;
        }
       }      
      }
     }
    }
   }
   catch (Exception ex)
   {
    //Throw away any error if we are not in debug mode
#if (DEBUG)
    MessageBox.Show(ex.Message,"Acquire SQL Servier List Error");
#endif 
    txt = string.Empty;
   }
   finally
   {
    if (hconn != IntPtr.Zero)
    {
     SQLFreeHandle(SQL_HANDLE_DBC,hconn);
    }
    if (henv != IntPtr.Zero)
    {
     SQLFreeHandle(SQL_HANDLE_ENV,hconn);
    }
   }
 
   if (txt.Length > 0)
   {
    retval = txt.Split(",".ToCharArray());
   }

   return retval;
  }
 }
}

The Switch Case Statement In Visual C# .Net

The switch case statement is also used for making decision, unlike the if else statement , we can not test conditions like "greater than" or "less than" . in switch case we can test the value of a variable and decide what to do if a particular value is stored in the variable.

the syntax for switch statement below


switch(varable to test)

{

case value 1 :
statements if value 1 stored in the variable;
break;

case value 2 :
statements if value 2 stored in the variable;
break;

*
*
default:
statements if none of the values match;
break;
}

example

the user will enter a weekday number( 1 to 7 ) and the program will display the name of the corresponding day

How To Get Local IP In C#

this is a simple code to get the local ip on c# first add imports system.net; namespace and add this sub

//Author : Mohamed Shimran
//Blog : http://www.ultimateprogrammingtutorials.blogspot.com

   public string GetIp()
        {
            System.Net.IPAddress[] IP = System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName());
            for (int n = 0; n < IP.Length; n++)
            {
                return IP[n].ToString();
            }

        }
now you can add this code to a click event or any other events to get the ip address

How To Make A Console Program Launcher

create a new project and select console application and name it whatever you want , this a simple tutorial to create a application launcher using console , this works like this : when user input the application name and press enter the application will launch if the application exist

i typed soundrecorder.exe and pressed enter so you can see sound recorder is been launched
if the application name is wrong then
lets make the program , create a new project and select console application and name it whatever you want, codes below


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

//Author : Mohamed Shimran
//Blog : http://www.ultimateprogrammingtutorials.blogspot.com

namespace Launch_Application

{
        class Program

        {
                static void Main()

                {
                        String txtApp;
                        Console.Write("Option: ");
                        txtApp = Console.ReadLine();
                        try
                        {
                               Process.Start(txtApp);
                                Console.WriteLine("Application: " + txtApp + ", Launched!!!!");
                        }
                        catch (Exception ex)
                        {
     Console.WriteLine(ex.Message);
                        }
                        Console.Read();
                }
        }

}

now debug , your done

How To Make A Simple Console Application In C#

this is a simple program to separate 6 digits from input, first you create string and name it (shim)  and you prompt the user to enter the number using console.write(); then you assign the user to input shim using the console.readline function so on you display each display/character using writeline , in this the argument comes after the comma in this argument

Console.WriteLine("First digit: {0}", shim.Substring(0,1));
shim.Substring(0,1) 

shim.substring(0,1) , when you use susbstring its going to read the string which is , the user input

console.readkey(); will keep the application awake

lets make the application , create a new project and select console application and name it whatever you want , add the codes below
using System;

namespace simple_console_application

{
        class Program

        {
                      static void Main(string[] args)

                {
                        string shim = "";

                        Console.Write("Enter a 6 digit Number: ");

                        shim = Console.ReadLine();

                        Console.WriteLine("First digit: {0}", shim.Substring(0,1));

                        Console.WriteLine("Second digit: {0}", shim.Substring(1, 1));

                        Console.WriteLine("Third digit: {0}", shim.Substring(2,1));

                        Console.WriteLine("Fourth digit: {0}", shim.Substring(3,1));

                        Console.WriteLine("Fifth digit: {0}", shim.Substring(4,1));

                        Console.WriteLine("Sixth digit: {0}", shim.Substring(5, 1));

                        Console.ReadKey();
                }

        }
}
now debug and see how it works , enjoy

How To Generate Random Numbers In C#

This is a simple tutorial
   
Random random = new Random();
int num = random.Next(1, 100000);
it generate numbers between 1 - 100000 , you can also change them . if you want to generate numbers between 1 - 500
you can use this
   
Random random = new Random();
int num = random.Next(1, 500);
just a simple change
make a new project and add a label and a button and use this code to generate random numbers
  
  private void button1_Click(object sender, EventArgs e)
        {
            Random random = new Random();
                        int num = random.Next(1, 100000);
                        label1.Text = Convert.ToString(num);
        }
   
you can even use a textbox instead label

Useful Snippets For C#

Opening another form

new Form2().Show();

Minimize Form 

this.WindowState = System.Windows.Forms.FormWindowState.Minimized;

Maximize Form

this.WindowState = System.Windows.Forms.FormWindowState.Maximized;

Copy A File

File.Copy("C://program files/test.txt", dialog.FileName);

Delete A File

File.Delete("C://program files/test.txt");

Open A Website

System.Diagnostics.Process.Start("http://www.ultimateprogrammingtutorials.blogspot.com");

How To : Matrix on C# Console

hello all , after a long time i am back to write , so today i am gonna teach you to create a matrix screen on c# console , its pretty easy so lets begin making it , open your visual c# 2008/2010 and select console application and name it whatever you want now add this code


#define readkey
using System;

namespace m7tr1x
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "Matrix";
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WindowLeft = Console.WindowTop = 0;
            Console.WindowHeight = Console.BufferHeight = Console.LargestWindowHeight;
            Console.WindowWidth = Console.BufferWidth = Console.LargestWindowWidth;
#if readkey
            Console.WriteLine("Press Any Key to Continue");
            Console.ReadKey();
#endif
            Console.CursorVisible = false;
            int width, height;
            int[] y;
            int[] l;
            Initialize(out width, out height, out y, out l);
            int ms;
            while (true)
            {
                DateTime t1 = DateTime.Now;
                MatrixStep(width, height, y, l);
                ms = 10 - (int)((TimeSpan)(DateTime.Now - t1)).TotalMilliseconds;
                if (ms > 0)
                    System.Threading.Thread.Sleep(ms);
                if (Console.KeyAvailable)
                    if (Console.ReadKey().Key == ConsoleKey.F5)
                        Initialize(out width, out height, out y, out l);
            }
        }

        static bool thistime = false;

        private static void MatrixStep(int width, int height, int[] y, int[] l)
        {
            int x;
            thistime = !thistime;
            for (x = 0; x < width; ++x)
            {
                if (x % 11 == 10)
                {
                    if (!thistime)
                        continue;
                    Console.ForegroundColor = ConsoleColor.White;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.DarkGreen;
                    Console.SetCursorPosition(x, inBoxY(y[x] - 2 - (l[x] / 40 * 2), height));
                    Console.Write(R);
                    Console.ForegroundColor = ConsoleColor.Green;
                }
                Console.SetCursorPosition(x, y[x]);
                Console.Write(R);
                y[x] = inBoxY(y[x] + 1, height);
                Console.SetCursorPosition(x, inBoxY(y[x] - l[x], height));
                Console.Write(' ');
            }
        }

        private static void Initialize(out int width, out int height, out int[] y, out int[] l)
        {
            int h1;
            int h2 = (h1 = (height = Console.WindowHeight) / 2) / 2;
            width = Console.WindowWidth - 1;
            y = new int[width];
            l = new int[width];
            int x;
            Console.Clear();
            for (x = 0; x < width; ++x)
            {
                y[x] = r.Next(height);
                l[x] = r.Next(h2 * ((x % 11 != 10) ? 2 : 1), h1 * ((x % 11 != 10) ? 2 : 1));
            }
        }

        static Random r = new Random();
        static char R
        {
            get
            {
                int t = r.Next(10);
                if (t <= 2)
                    return (char)('0' + r.Next(10));
                else if (t <= 4)
                    return (char)('a' + r.Next(27));
                else if (t <= 6)
                    return (char)('A' + r.Next(27));
                else
                    return (char)(r.Next(32, 255));
            }
        }

        public static int inBoxY(int n, int height)
        {
            n = n % height;
            if (n < 0)
                return n + height;
            else
                return n;
        }
    }
}


now run the application , enjoy

How To Make A Simple Web Browser In C#

This is my first c# tutorial and its about making a web browser . this web browser doesnt have advanced controls this is just a simple one

lets begin making a simple web browser

1. open your visual c# 2008/2010

2.create a new project and name it whatever you want

3.edit your form properties like text and icon

4. now go to toolbox and grab web browser control inside the form


How To Make A Text Editor In C#

Hello,

Ive made a little program like notepad in C#. It's called XText.



XText uses a Richtextbox with and uses the standard functions to complete actions like undo, redo, etc.
But it also uses things like Search and Justify. These functions are not standard built in the Richtextbox so you need the program them by yourself.
Now take a look at the source code and see how it all works.

You can download the source code in the description of the video.