Archive for the Category code


Invoking the Microsoft Log Parser COM Interface from managed code ( C# )

This will create an XML file that has your url’s that are refered to, hit count, and the referrer.

    Type comLogQueryType = 
        Type.GetTypeFromProgID("MSUtil.LogQuery", true);
    object comLogQueryObject = 
    // Get the IIS Input and XML output filters
    Type ws3LogType = 
        Type.GetTypeFromProgID("MSUtil.LogQuery.IISW3CInputFormat", true);
    object ws3LogObject = 
    Type xmlLogType = 
        Type.GetTypeFromProgID("MSUtil.LogQuery.XMLOutputFormat", true);
    object xmlLogObject = 
    // Setup input and output files
    string inPath = "someIISlog.log";
    string outpath = "temp.xml";
    // Create a SQL query to get the referers, count and uri-to. Order by total hits
    string query = 
        "SELECT cs(Referer) as Referer,cs-uri-stem as To,COUNT(*) as Total from " +
        inPath + " TO " + outpath +
        " WHERE (sc-status=200) AND (Referer LIKE 'http:%') GROUP BY Referer,To ORDER BY Total DESC";
    // Invoke the ExcuteBatch method
    object[] inputArgs = {query, ws3LogObject, xmlLogObject};
    comLogQueryType.InvokeMember("ExecuteBatch", BindingFlags.InvokeMethod, 
        null, comLogQueryObject, inputArgs);
catch (Exception e)
    string errorString = "An exception has occurred: " + e.Message;
[Via Steve Makofsky ]

DataAdapter for CSV files

Cathi Gero posted some code that allows you work with CSV files using CSVDataAdapter . Nice job!

To read the contents of a CSV file:

CSVDataAdapter CSVda = new CSVDataAdapter(@"c:\MyFile.csv");
CSVda.HasHeaderRow = true;
DataSet ds = new DataSet();

To write to a CSV file:

CSVDataAdapter CSVda = new CSVDataAdapter(@"c:\MyFile.csv");
bool InclHeader = true;

Aggregated reflection-based ToString() implementation

Instead displaying your object’s public fields(properties) in ordinary way ,that doesn’t so informative

string foo = string.Format( "My name is {0} {1} and my birthday is {2:MM/dd/yyyy}", 
                            p.FirstName, p.LastName, p.BirthDate );

you can print your object using FormattableObject library

Person p = new Person( "Scott","Hanselman",new DateTime(1974,1,22) );
string foo = p.ToString("My name is {FirstName} {LastName} and my birthday is {Birthdate:MM/dd/yyyy}");

Download FormattableObject 0.5

[ Via ]

The CLR team’s internal coding guidelines

Brad Abrams just published an article containing the CLR team’s internal coding conventions . It’s a good example of a coding style document.

[ Via Larry Osterman's WebLog ]

How to programmatically retrieve the atom feed from gmail

Gmail has added Atom web feeds, a format that's similar to RSS.The feeds include a summary of each new message in your Google email.Below you can find how to programmatically retrieve the atom feed from gmail.


using System;
using System.IO;
using System.Net; 
WebClient client = new WebClient();
client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
client.Credentials = new NetworkCredential("loginname","password"); 
Stream data = client.OpenRead ("");
StreamReader reader = new StreamReader (data);
string s = reader.ReadToEnd (); 
Console.WriteLine (s); 
data.Close ();
reader.Close ();

Page 4 of 512345