Archive for Maggio 5th, 2008

Parse CSV file to list

CodeKeep C# Feed Maggio 5th, 2008

Description: Parse a delimited text file into a list

Link: http://www.codekeep.net/snippets/ad44f11b-e00a-4bfa-9d61-ba360cf6b8ba.aspx

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO; //System.IO is not used by default

public List<string[]> parseCSV(string path)
{
  List<string[]> parsedData = new List<string[]>();

  try
  {
    using (StreamReader readFile = new StreamReader(path))
    {
      string line;
      string[] row;

      while ((line = readFile.ReadLine()) != null)
      {
        row = line.Split(',');
        parsedData.Add(row);
      }
    }
  }
  catch (Exception e)
  {
    MessageBox.Show(e.Message);
  }

  return parsedData;
}

LINQ to find file

CodeKeep C# Feed Maggio 5th, 2008

Description: LINQ to find a given filename / pattern / etc

Link: http://www.codekeep.net/snippets/5195652d-2810-4006-809c-b4bcdaf699cd.aspx

var files = from file in new DirectoryInfo(@"K:\SomeFolder").GetFiles()
    where file.Name == "SomeFileName.txt"
        select file;

LINQ to search for a directory

CodeKeep C# Feed Maggio 5th, 2008

Description: Using LINQ to search for a directory

Link: http://www.codekeep.net/snippets/8201d373-50bf-41d5-825b-43e6a4e1fad6.aspx

var folders = from folder in new DirectoryInfo(@"K:\SomeFolder").GetDirectories()
	where folder.Name == "SubFolderName"
        select folder;

LINQ to text file

CodeKeep C# Feed Maggio 5th, 2008

Description: Use LINQ to read

Link: http://www.codekeep.net/snippets/a48cd7ec-e571-4d20-8995-cd35186a8d0e.aspx

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Query;

namespace LinqToText
{
  public static class StreamReaderSequence
  {
    public static IEnumerable<string> Lines(this StreamReader source)
    {
      String line;

      if (source == null)
          throw new ArgumentNullException("source");
      while ((line = source.ReadLine()) != null)
      {
        yield return line;
      }
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      StreamReader sr = new StreamReader("TextFile.txt");

      var t1 =
        from line in sr.Lines()
        let items = line.Split(',')
        where ! line.StartsWith("#")
        select String.Format("{0}{1}{2}",
            items[1].PadRight(16),
            items[2].PadRight(16),
            items[3].PadRight(16));

      var t2 =
        from line in t1
        select line.ToUpper();

      foreach (var t in t2)
        Console.WriteLine(t);

      sr.Close();
    }
  }
}

How to get the proper Windows or Program Files parth. Also several other Special Folders.

CodeKeep C# Feed Maggio 5th, 2008

Description: This is well known by many but took me a while to discover when new to C#. There quite a list in the enumeration of special folders

Link: http://www.codekeep.net/snippets/5203acc2-8e47-4972-ac6f-3fc4e85b514f.aspx

Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
Environment.GetFolderPath(Environment.SpecialFolder.System);
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

How to Create an Ajax Autocomplete Text Field: Part 5

WebReference News Maggio 5th, 2008

In part 4 of the series, we defined the appearance of our autocomplete control using an external cascading style sheet (CSS). This week we’ll create a JavaScript file to manage the behavior of the autocomplete control in the browser. By Rob Gravelle. 0501