Archive for Maggio 6th, 2008

sort with IComparer

CodeKeep C# Feed Maggio 6th, 2008

Description: sort with IComparer

Link: http://www.codekeep.net/snippets/f6a99ed6-b00a-4a6f-b9c6-35391156349e.aspx

  public class SecurityComparer : IComparer<Security>
    {
        public int Compare(Security a, Security b)
        {
            Debug.Assert(   a != null && b != null );
            return a.Expiration.CompareTo(b.Expiration);
        }
    }

OpenDialogBox with filter

CodeKeep C# Feed Maggio 6th, 2008

Description: Sample with filter

Link: http://www.codekeep.net/snippets/33681c47-eded-48a1-9dc2-30b5a4207d10.aspx

// coloca o título na janela
    this.openfiledialog1.title = "escolha o banco de dados...";
    // informa qual será o diretório inicial
    this.openfiledialog1.initialdirectory = "c:\\";
    // configura o filtro para tipo de arquivo
    this.openfiledialog1.filter = "txt files (*.txt)|*.txt";
    // abre a janela para escolher o banco de dados
    this.openfiledialog1.showdialog();

    // pega o caminho escolhido
    this.tbx_caminho.text = this.openfiledialog1.filename;

Combine LINQ Queries with Regular Expressions

CodeKeep C# Feed Maggio 6th, 2008

Description: This example shows how to use the Regex class to create a regular expression for more complex matching in text strings.

Link: http://www.codekeep.net/snippets/0f080f48-ec9c-426a-9ce4-b9b12f164195.aspx

class QueryWithRegEx
{
    public static void Main()
    {
        // Modify this path as necessary.
        string startFolder = @"c:\program files\Microsoft Visual Studio 9.0\";

        // Take a snapshot of the file system.
        IEnumerable<System.IO.FileInfo> fileList = GetFiles(startFolder);

        // Create the regular expression to find all things "Visual".
        System.Text.RegularExpressions.Regex searchTerm =
            new System.Text.RegularExpressions.Regex(@"Visual (Basic|C#|C\+\+|J#|SourceSafe|Studio)");

        // Search the contents of each .htm file.
        // Remove the where clause to find even more matches!
        // This query produces a list of files where a match
        // was found, and a list of the matches in that file.
        // Note: Explicit typing of "Match" in select clause.
        // This is required because MatchCollection is not a
        // generic IEnumerable collection.
        var queryMatchingFiles =
            from file in fileList
            where file.Extension == ".htm"
            let fileText = System.IO.File.ReadAllText(file.FullName)
            let matches = searchTerm.Matches(fileText)
            where searchTerm.Matches(fileText).Count > 0
            select new
            {
                name = file.FullName,
                matches = from System.Text.RegularExpressions.Match match in matches
                          select match.Value
            };

        // Execute the query.
        Console.WriteLine("The term \"{0}\" was found in:", searchTerm.ToString());

        foreach (var v in queryMatchingFiles)
        {
            // Trim the path a bit, then write
            // the file name in which a match was found.
            string s = v.name.Substring(startFolder.Length - 1);
            Console.WriteLine(s);

            // For this file, write out all the matching strings
            foreach (var v2 in v.matches)
            {
                Console.WriteLine("  " + v2);
            }
        }

        // Keep the console window open in debug mode
        Console.WriteLine("Press any key to exit");
        Console.ReadKey();
    }

    // This method assumes that the application has discovery
    // permissions for all folders under the specified path.
    static IEnumerable<System.IO.FileInfo> GetFiles(string path)
    {
        if (!System.IO.Directory.Exists(path))
            throw new System.IO.DirectoryNotFoundException();

        string[] fileNames = null;
        List<System.IO.FileInfo> files = new List<System.IO.FileInfo>();

        fileNames = System.IO.Directory.GetFiles(path, "*.*", System.IO.SearchOption.AllDirectories);
        foreach (string name in fileNames)
        {
            files.Add(new System.IO.FileInfo(name));
        }
        return files;
    }
}