How to Find All SQL Server Instance Running in Local Network c#

Create new class its name to GetInstance and write this below code on GetInstance class.

using System.Data.Sql;
using System.Collections;
using System.Data;

namespace SqlServer
{
    public class GetInstance
    {
        public static ArrayList GetInstanceName()
        {
            try
            {
                SqlServerList SqlSL = new SqlServerList();
                SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
                DataTable table = instance.GetDataSources();
                ArrayList list = new ArrayList();
                foreach (DataRow row in table.Rows)
                {
                    SqlSL = new SqlServerList();
                    SqlSL.ServerName = row[0].ToString();
                    SqlSL.InstanceName = row[1].ToString();
                    SqlSL.IsClustered = row[2].ToString();
                    SqlSL.Version = row[3].ToString();
                    list.Add(SqlSL);
                }
                return list;
            }
            catch
            {
                return null;
            }
        }
    }
}

 

Create new class its name to SqlServerList and write this below code on SqlServerList class.
Continue reading How to Find All SQL Server Instance Running in Local Network c#

How To enable Aero in Windows Server 2008

 Windows Media Encoder Desktop Experience Component Installed

 

To install the Windows Media Encoder Desktop Experience component by using the Windows Server 2008 Server Manager

  1. Click Start, click Administrative Tools, and then click Server Manager.
  2. In the left navigation pane, click Features, and then right click and select Add Features.
  3. On the Select Features pane, select the check box for Desktop Experience.
  4. Click Next from the Select Features pane, and then click Install at the Confirm Installations Selections pane.
  5. Click Close to leave the Add Features wizard.
  6. The server must be restarted for the changes to take effect.

Continue reading How To enable Aero in Windows Server 2008

How to Drag a image from a pictureBox to the pictureBox

Insert a PictureBox and change its name to ImageSource and write this below code on MouseMove event handler.

private void ImageSource_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left & ImageSource.Image != null)
            {

                ImageSource.DoDragDrop(ImageSource.Image, DragDropEffects.All);
            }
        }

Insert a PictureBox and change its name to ImageDest and write this below code on DragEnter event handler.
Continue reading How to Drag a image from a pictureBox to the pictureBox

Email address validation by regular expressions C#

Import System.Text.RegularExpressions namespace.

using System.Text.RegularExpressions;
public bool IsEmailValid(string Address)
{
    string pattern = @"^(([^<>()[\]\\.,;:\s@\""]+"
          + @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"
          + @"
((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
          + @"
\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"
          + @"
[a-zA-Z]{2,}))$";
    Regex rest = new Regex(pattern);
    bool isrest Match = rest .IsMatch(Address);
    return isrest;
}

Clear All textbox in group panel

My answer about this question which , how you can clear all the text box inside a group panel

Answer:

private void cleartextboxes()
{
Control Alltext;
    foreach (Control tempLoop_Alltext in GroupBox1.Controls)
    {
      Alltext = tempLoop_Alltext;
      if (Alltext isTextBox)
         {
            alltext.Text = string.Empty;
         }
    }
}

Calculating the overall size of a directory

Import System.IO namespace

using System.IO;

Create new class its name to CalculateDirectorySize and write this below code on CalculateDirectorySize class.

public long CalculateDirectorySize(DirectoryInfo directorypath, bool includeSubdirectories)
{
    long totalSize = 0;
    // Examine all contained files.
    FileInfo[] files = directory.GetFiles();
    foreach (FileInfo file in files)
    {
        totalSize += file.Length;
    }
    // Examine all contained directories.
    if (includeSubdirectories)
    {
        DirectoryInfo[] dirs = directorypath.GetDirectories();
        foreach (DirectoryInfo dir in dirs)
        {
            totalSize += CalculateDirectorySize(dir, true);
        }
    }
    return totalSize;
}

Add a Button and change its name to GetCalculate and write this below code on Click event handler.

private void GetCalculate_Click(object sender, EventArgs e)
{
    MessageBox.Show(CalculateDirectorySize(new DirectoryInfo( @"C:\WINDOWS"), true).ToString());
}

Get all the fonts installed on the system

Import System.Drawing.Text namespace

using System.Drawing.Text;

Add a DropDownList and change its name to ListAllFont and write this below code on Form’s load event handler.

private void Form1_Load (object sender, System.EventArgs e)
{
InstalledFontCollection fonts = new InstalledFontCollection();
foreach (FontFamily font in fonts.Families)
    //ListAllFont is ListBox
    ListAllFont.Items.Add(font.Name);
}

String Manipulation in C#

Trim Function
The trim function has three variations Trim, TrimStart and TrimEnd. The first example show how to use the Trim(). It strips all white spaces from both the start and end of the string.

//STRIPS WHITE SPACES FROM BOTH START + FINSIHE
string Name = " String Manipulation " ;
string NewName = Name.Trim();
//ADD BRACKET SO YOU CAN SEE TRIM HAS WORKED
MessageBox.Show("["+ NewName + "]");

Continue reading String Manipulation in C#