Get selected and set selected in ListBox

// Loop through all items the ListBox.

for (int x = 0; x < listBox1.Items.Count; x++){

// Determine if the item is selected.

if(listBox1.GetSelected(x) == true)

// Deselect all items that are selected.

listBox1.SetSelected(x,false);

else

// Select all items that are not selected.

listBox1.SetSelected(x,true);

}

Anuncios

Avoid getting NULL value

if(!IsPostBack){

//code here

}

Get IP information

bool pingable = false;

Ping pinger = new Ping();

try{

PingReply reply = pinger.Send(“XXX.XXX.X.XX”);

pingable = reply.Status == IPStatus.Success;

textbox1.Text = “Status:” + pingable.ToString() + Environment.NewLine + “Address: ” + reply.Address + Environment.NewLine + “Time in milisecond:” + reply.RoundtripTime;

}catch(PingException){

//Discard PingExceptions and return false;

}

Check if a process is opened

1st way:

foreach(Process clsProcess in Process.GetProcesses()){

if(clsProcess.ProcessName.Contains(processNameWithoutEXE)){

return true;

return false;

}

}

 

2nd way:

Process[] pname = Process.GetProcessesByName(“notepad”);

if (pname.Length == 0)

MessageBox.Show(“nothing”);

else

MessageBox.Show(“run”);

Get files from directory

string[] filePaths = Directory.GetFiles(@”C:\MyDir\”);

 

With specified extension:

string[] filePaths = Directory.GetFiles(@”C:\MyDir\”, “*.bmp”);

 

Include all subdirectories:

string[] filePaths = Directory.GetFiles(@”C:\MyDir\”, “*.bmp”, SearchOption.AllDirectories);