Printing a Remote Document or Url Using C#
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: .net

1 Comments:
ks for the post. It helped me a lot
By
Bijoy Thangaraj, at 1:48 AM
Post a Comment
<< Home