Articoli con tag how to
Come rimuovere tutti i tag HTML da una stringa con Visual C#
0L’esempio riportato di seguito mostra un metodo molto semplice per rimuovere tutti i tag HTML, presenti in una stringa, utilizzando Visual C#(sharp) e le Regular Expressions :
using System.Text.RegularExpressions;
...
const string HTML_TAG_PATTERN = "<.*?>";
static string StripHTML (string inputString)
{
return Regex.Replace(inputString, HTML_TAG_PATTERN, string.Empty);
}
Come eliminare i file temporanei di Internet con Visual C#
0Ecco una semplice procedura per eliminare la cache di Microsoft Internet Explorer con Visual C#
using System.IO;
...
void clearIECache()
{
ClearFolder (new DirectoryInfo (Environment.GetFolderPath
(Environment.SpecialFolder.InternetCache)));
}
void ClearFolder (DirectoryInfo folder)
{
foreach (FileInfo file in folder.GetFiles())
{ file.Delete(); }
foreach (DirectoryInfo subfolder in folder.GetDirectories())
{ ClearFolder(subfolder); }
}
public static void Main( )
{
new Test().clearIECache ();
}
Come visualizzare tutti i nomi dei database Sql Server con Visual C#
0Nell’esmpio riportato di seguito, vi riporto una semplice procedura per recuperare tutti i nomi dei database sql server utilizzando Visual C# :
using System.Data;
using System.Data.SqlClient;
...
// Sostituire con la propria stringa di connessione
String conxString = "Data Source=MYSERVER; Integrated Security=True;";
using (SqlConnection sqlConx = new SqlConnection (conxString))
{
sqlConx.Open();
DataTable tblDatabases = sqlConx.GetSchema ("Databases");
sqlConx.Close();
foreach (DataRow row in tblDatabases.Rows)
{
Console.WriteLine ("Database: " + row["database_name"]);
}
}
Come scrivere una chiave nel registro di Windows con Visual C#
0
using Microsoft.Win32;
...
RegistryKey masterKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Test\\Preferences");
if (masterKey == null)
{
Console.WriteLine ("Null Masterkey!");
}
else
{
try
{
masterKey.SetValue ("MyKey", "MyValue");
}
catch (Exception ex)
{
Console.WriteLine (ex.Message);
}
finally
{
masterKey.Close();
}
}
Come leggere una chiave dal registro di Windows con Visual C#
0Nell’ esempio che segue viene mostrato come leggere una chiave del registro di Windows utilizzando Visual C# ed il .Net Framework :
using Microsoft.Win32;
...
RegistryKey masterKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Test\\Preferences");
if (masterKey == null)
{
Console.WriteLine ("Null Masterkey!");
}
else
{
Console.WriteLine ("MyKey = {0}", masterKey.GetValue ("MyKey"));
}
masterKey.Close();
[C#] : Come scansionare un Array con LINQ
0
using System;
using System.Collections;
using System.Collections.Generic;
namespace ForEachExaple
{
public static class Utility
{
public static void ForEach(this IEnumerable coll, Action function)
{
IEnumerator en = coll.GetEnumerator();
while (en.MoveNext())
function(en.Current);
}
}
class Program
{
static void Main(string[] args)
{
var array = new int[] { 1, 2, 3 };
//Output : 123
array.ForEach(val => Console.Write(val));
}
}
}
[C# Custom Controls] : Come creare una TextBox che accetti solo caratteri di tipo numerico
1Come prima cosa aggiungiamo al nostro progetto una nuova classe che chiameremo “NumericTextBox” facendo in modo che la stessa erediti dalla classe TextBox in questo modo :
namespace ControlliPersonalizzati
{
public class NumericTextBox : TextBox{ }
}
Ora aggiungiamo una proprietà alla classe di tipo bool che ci servirà ad indicare se nella TextBox sono permessi o meno numeri negativi :
public bool AccettaNegativi { get; set; }
A questo punto dobbiamo intercettare l’evento KeyPress del textbox, e lo facciamo nel costruttore della nostra classe in questo modo :
public NumericTextBox()
{
this.KeyPress += new KeyPressEventHandler(NumericTextBox_KeyPress);
}


![Last Minute last60 225x300 [C#] : Come scansionare un Array con LINQ](http://www.luigimelisi.com/wp-content/uploads/2010/06/last60-225x300.jpg)