Kris Krause .NET Meister

"If it is fast and ugly, they will use it and curse you; if it is slow, they will not use it."
- David Cheriton, The Art of Computer Systems Performance Analysis

Tuesday, February 08, 2005

Printing a Remote Document or Url Using C#

The following code enables a local machine to print a remote document or Url. One example would be a desktop application that needs to print a remote PDF or a Html producing web page.

The simple example downloads the file locally in order to print using the default printer. Sure there might be a better way to do this... and the code needs to be modified to handle "file not found", "url not found", and "unable to connect" exceptions in case a cable is loose. Cable? Who uses cable? It's all wireless nowadays!

using System;
using System.Diagnostics;
using System.Net;

namespace ShellPrint
{
class CMain
{
[STAThread]
static void Main(string[] args)
{
string lfile, file, ext;
Process proc = null;
WebClient client = null;

try
{
if (args.Length < 2) throw new ArgumentException("Invalid arguments passed.");

file = args[1].Trim();
ext = args[0].Trim();

client = new WebClient();

lfile = @"c:\" + DateTime.Now.Ticks + "." + ext;

Console.WriteLine("Downloading: {0}", file);

client.DownloadFile(file, lfile);

Console.WriteLine("Saving: {0}", file);

proc = new Process();

proc.StartInfo.FileName = lfile; 
proc.StartInfo.Verb = "Print";
proc.StartInfo.CreateNoWindow = true;

Console.WriteLine("Printing: {0}", lfile);

proc.Start();
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
Console.WriteLine("Done and done.");
Console.ReadLine();
}
}
}
}

Labels:

Wednesday, February 02, 2005

Accessing iTunes With C#

The following example dumps your entire iTunes library to the console. You need to reference the COM iTunes 1.2 Type library. Currently I am running iTunes version 4.7.1.30.

using System;

namespace PlayListExample
{
class CMain
{
[STAThread]
static void Main(string[] args)
{
iTunesLib.IiTunes app = null;

try
{

app = new iTunesLib.iTunesAppClass();

foreach (iTunesLib.IITTrack track in app.LibraryPlaylist.Tracks)
{
Console.WriteLine("Artist: {0}\nSong: {1}\nGenre: {2}\n", track.Artist, track.Name, track.Genre);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
app.Quit();

app = null;

Console.ReadLine();
}
}
}
}

Labels: ,

Parsing dBase .DBF to .CSV Using C# and VB.NET

Per a New Jersey Microsoft Developer's Group message board request I created a sample console application that parses a .DBF file and creates an Excel usable .CSV file. Zero magic if you take advantage of the System.Data.Odbc namespace.

The command line syntax is:

FPParse.exe [table name] [output dir]
FPParse.exe canada c:

The generated .CSV file is just as good as an .XLS file. For fancy .XLS features and formatting I suggest modifying my code to utilize Excel COM Interop.

The C# source code is found here.

The VB.NET source code is found here.

Labels: