Archive

Posts Tagged ‘C#’

Central Error Tracking for Deployed Systems

January 11th, 2010 Matt J. Wilson No comments

At one of my previous employers, we had several remote deployments (hundreds, actually) and a need to track remote errors in a central location; we utilized an ASP site to visually display this combined collection of errors.

I’ve taken the time to rewrite my own central error tracking mechanism from scratch. This module includes text logs that automatically rotate themselves once a file reaches a certain size (no more trying to have someone email you a 3 GB text file), useful information about what type of error occurred, and a SQL CE database that stores these errors in a local database, counting the number of incidences and whether there is fresh information to report on a schedule to the central database.

There is also a central web service that accepts these exception reporting requests and inserts them into a central SQL database for you to do with what you will (ASP website, Excel sheets with queries, etc., etc.). You can find a copy of this solution (Visual Studio 2005 written in C#) here.

Categories: Programming Tags: , ,

Address Pattern Regular Expression

July 14th, 2009 Matt J. Wilson No comments

So I searched the internet for quite a while to find a street address regex that actually worked (and worked well).  I didn’t manage to accomplish this, so I wrote my own.  I’m keeping the address extraction algorithms and logic private, but I figured I could at least share the first part of the Regex:

Regex addressPattern = new Regex(@"(?<city>[A-Za-z',.\s]+) (?<state>([A-Za-z]{2}|[A-Za-z]{2},))\s*(?<zip>\d{5}(-\d{4})|\d{5})");

MatchCollection matches = addressPattern.Matches(text);

for (int mc = 0; mc < matches.Count; mc++)
{
string city =  matches[mc].Groups["city"].Value;
string state =  matches[mc].Groups["state"].Value;
string zip =  matches[mc].Groups["zip"].Value;
}

[/c-sharp]

Categories: Programming Tags: ,

Posting a File using the WebBrowser Class

February 12th, 2009 Matt J. Wilson 1 comment

So you have a WebBrowser object in .NET that you would like to use to automatically post a file to a given location?  You can probably figure out how to set values on this page:

<input type="text" name="myTextField">
// Update myTextField with a value
webBrowser.Document.All["myTextField"].SetAttribute("value", "I set your value");

But how do you update this?

<input type="file" name="myFileField">

You can’t simply use the SetAttribute function, as there is no attribute to correctly set; this was done intentionally to prevent users from using shady Javascript to automatically capture and send files from a page.  So how do you do it?  Something even more shady:

HtmlElement element = webBrowser.Document.GetElementById("myFileField");
element.Focus();
System.Threading.Thread.Sleep(1000);

// Perform SendKeys to fake the browser into setting the file name
SendKeys.SendWait(@"C:\Path To My File\test.txt");
SendKeys.Flush();

I hope that Google picks this up and saves someone else the thirty minutes worth of their life that I wasted attempting to figure out a solution.

Categories: Programming Tags: , ,