Archive for Gennaio 21st, 2008

Email Regular Expression

CodeKeep C# Feed Gennaio 21st, 2008

Description: Email

Link: http://www.codekeep.net/snippets/8606128f-1016-46e6-9dad-cfc012f55575.aspx

Valid Email Regular Expression

ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"

Email Regular Expression

CodeKeep C# Feed Gennaio 21st, 2008

Description: Email

Link: http://www.codekeep.net/snippets/c0f35d35-2cfa-4b9c-aa90-f234792ddb18.aspx

Valid Email Regular Expression

ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"

Get Embedded Image from Assembly

CodeKeep C# Feed Gennaio 21st, 2008

Description: Gets an embedded image from the current assembly. Embedded images are found in the assembly manifest.

Link: http://www.codekeep.net/snippets/9e9119a7-56a5-4682-b736-040e65160352.aspx

using System.Reflection;
using System.IO;

/// <summary>
/// Gets an embedded image from the current assembly.
/// </summary>
/// <param name="imagePath">Full namespace path of the image.</param>
/// <returns>The image found at <paramref name="imagePath"/>.</returns>
/// <example>
/// The root namespace of your project is MyCompany.MyApp and you have
/// an image named foo.png in a folder named Images. The imagePath would
/// be MyCompany.MyApp.Images.foo.png.
/// </example>
public Image GetEmbeddedImage(string imagePath)
{
    // Embedded items are found in the assembly manifest
    Assembly thisAssembly = Assembly.GetAssembly(this.GetType());
    Stream resourceStream = thisAssembly.GetManifestResourceStream(imagePath);
    return Image.FromStream(resourceStream);
}

Esempio di creazione comandi

CodeKeep C# Feed Gennaio 21st, 2008

Description: CommandBar IDE

Link: http://www.codekeep.net/snippets/18e64c22-78a9-44ee-b8c8-50d20c0d9a67.aspx

				//Inserisce il comando nel menu degli strumenti.
				//Individua la barra dei comandi MenuBar, che è la barra dei comandi di primo livello contenente tutte le voci di menu principali:
				Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"];

				//Individua la barra dei comandi Tools sulla barra dei comandi MenuBar:
				CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName];
				CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;

				//Se si desidera aggiungere più comandi da gestire con il componente aggiuntivo, questo blocco try/catch può essere duplicato,
				//  ma occorre ricordare di aggiornare anche il metodo QueryStatus/Exec per includere i nomi dei nuovi comandi.
				try
				{
					//Aggiunge un comando all'insieme Commands:
					Command command = commands.AddNamedCommand2(_addInInstance, "ConnectSample", "ConnectSample", "Executes the command for ConnectSample", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);

					//Aggiunge un controllo per il comando al menu degli strumenti:
					if((command != null) && (toolsPopup != null))
					{
						command.AddControl(toolsPopup.CommandBar, 1);
					}
				}
				catch(System.ArgumentException)
				{
					//In questo caso, probabilmente l'eccezione è stata generata perché un comando con quel nome
					//  esiste già. In tal caso non è necessario ricreare il comando ed è possibile
                    //  ignorare l'eccezione.
				}

Esempio di creazione comandi

CodeKeep C# Feed Gennaio 21st, 2008

Description: CommandBar IDE

Link: http://www.codekeep.net/snippets/18e64c22-78a9-44ee-b8c8-50d20c0d9a67.aspx

				//Inserisce il comando nel menu degli strumenti.
				//Individua la barra dei comandi MenuBar, che è la barra dei comandi di primo livello contenente tutte le voci di menu principali:
				Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"];

				//Individua la barra dei comandi Tools sulla barra dei comandi MenuBar:
				CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName];
				CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;

				//Se si desidera aggiungere più comandi da gestire con il componente aggiuntivo, questo blocco try/catch può essere duplicato,
				//  ma occorre ricordare di aggiornare anche il metodo QueryStatus/Exec per includere i nomi dei nuovi comandi.
				try
				{
					//Aggiunge un comando all'insieme Commands:
					Command command = commands.AddNamedCommand2(_addInInstance, "ConnectSample", "ConnectSample", "Executes the command for ConnectSample", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);

					//Aggiunge un controllo per il comando al menu degli strumenti:
					if((command != null) && (toolsPopup != null))
					{
						command.AddControl(toolsPopup.CommandBar, 1);
					}
				}
				catch(System.ArgumentException)
				{
					//In questo caso, probabilmente l'eccezione è stata generata perché un comando con quel nome
					//  esiste già. In tal caso non è necessario ricreare il comando ed è possibile 
                    //  ignorare l'eccezione.
				}

localizzazione

CodeKeep C# Feed Gennaio 21st, 2008

Description: legger in un file di risorse in base alla localizzazione

Link: http://www.codekeep.net/snippets/334b0aeb-fc7e-4d86-9dcd-fcf10c7246f1.aspx

				try
				{
					//Per spostare il comando in un menu differente, sostituire il termine "Tools" con la 
					//  versione inglese del menu. Il codice recupera la lingua, aggiunge il nome del menu,
					//  quindi aggiunge il comando al menu. Un elenco di tutti i menu di primo livello è contenuto nel file
					//  CommandBar.resx.
					ResourceManager resourceManager = new ResourceManager("ConnectSample.CommandBar", Assembly.GetExecutingAssembly());
					CultureInfo cultureInfo = new System.Globalization.CultureInfo(_applicationObject.LocaleID);
					string resourceName = String.Concat(cultureInfo.TwoLetterISOLanguageName, "Tools");
					toolsMenuName = resourceManager.GetString(resourceName);
				}
				catch
				{
					//Non è stata trovata alcuna versione localizzata del termine Tools.
					//  Imposta come predefinito il termine inglese, che può funzionare con la lingua corrente.
					toolsMenuName = "Tools";
				}

Xml Dataset

CodeKeep C# Feed Gennaio 21st, 2008

Description: lettura dati Xml da un dataset

Link: http://www.codekeep.net/snippets/fa6d9975-c718-407f-91c7-f39f62eef89e.aspx

    XmlDataDocument config_file;
                XmlTextReader xtr = new XmlTextReader(XML_file);
                config_file = new XmlDataDocument();
                config_file.DataSet.ReadXmlSchema(xtr);
                xtr.Close();
                config_file.Load(XML_file);
            

XmlEscape

CodeKeep C# Feed Gennaio 21st, 2008

Description: escape di caratteri per XML

Link: http://www.codekeep.net/snippets/6b75dab8-ef30-4d5a-b320-aa6987f7122d.aspx

        public static string XMLEscape(string str)
        {
            return str.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("\"", "&quot;").Replace("'", "&apos;");
        }

Lettura di u file di testo tramite ODBC

CodeKeep C# Feed Gennaio 21st, 2008

Description: viene sfruttato il supporto per il linguaggio SQL

Link: http://www.codekeep.net/snippets/5b2f47ac-2bb4-4aa6-b63e-389b6be266d3.aspx

				string text = File.ReadAllText(file_name);
				text = text.Replace("'", "''");
				File.WriteAllText(file_name, text);

				conn = new OdbcConnection("Provider=MSDASQL;DSN=" + DSN + ";");
				conn.Open();
				ds = new DataSet();
				OdbcDataAdapter adapter = new OdbcDataAdapter();
				adapter.SelectCommand = new OdbcCommand("SELECT " + ((distinct_clause) ? "DISTINCT" : "") + " * FROM " + file_name + ((where_condition != "" && where_condition != null) ? " WHERE " + where_condition : "") + ((ordering != "" && ordering != null) ? " ORDER BY " + ordering : ""), conn);
				adapter.Fill(ds);

Lettura di u file di testo tramite ODBC

CodeKeep C# Feed Gennaio 21st, 2008

Description: viene sfruttato il supporto per il linguaggio SQL

Link: http://www.codekeep.net/snippets/5b2f47ac-2bb4-4aa6-b63e-389b6be266d3.aspx

				string text = File.ReadAllText(file_name);
				text = text.Replace("'", "''");
				File.WriteAllText(file_name, text);

				conn = new OdbcConnection("Provider=MSDASQL;DSN=" + DSN + ";");
				conn.Open();
				ds = new DataSet();
				OdbcDataAdapter adapter = new OdbcDataAdapter();
				adapter.SelectCommand = new OdbcCommand("SELECT " + ((distinct_clause) ? "DISTINCT" : "") + " * FROM " + file_name + ((where_condition != "" && where_condition != null) ? " WHERE " + where_condition : "") + ((ordering != "" && ordering != null) ? " ORDER BY " + ordering : ""), conn);
				adapter.Fill(ds);

Next »