Archive for the ‘.NET C#’ Category

Just some links that help with programming SMTP Sinks for IIS.

  1. SMTP Server Technical Articles Microsoft Windows 2000 SMTP Service Events
    <http://msdn.microsoft.com/en-us/library/ms998608.aspx>
  2. How to write an OnArrival-type SMTP event sink in managed code by using Visual Studio .NET 2003
    <http://support.microsoft.com/kb/894286>
  3. Managed IIS SMTP Sink Wrapper: Message.CopyContentToStream
    <http://codeka.com/blogs/index.php/2005/06/06/managed_iis_smtp_sink_wrapper_message_co>
  4. C# Catchall Onarrival Event sink
    <http://gsexdev.blogspot.com/2005/05/c-catchall-onarrival-event-sink.html>
  5. ESTMP Keywords and Verbs (commands) Define
    <http://smtpfilter.sourceforge.net/esmtp.html>
  6. How to get the IP of the SMTP client
    <http://codeka.com/blogs/index.php/2007/08/08/how_to_get_the_ip_of_the_smtp_client>
  7. Sample Sink
    <http://svn.vestris.com/filedetails.php?repname=Vestris+SVN&path=%2Fsncore%2FSnCore.MailSink%2FSnCore.DomainMail%2FSink.cs&rev=1289 >
  8. Creating a custom authentication sink for IIS SMTP or Exchange
    <http://blog.rednael.com/2008/08/13/CreatingACustomAuthenticationSinkForIISSMTPOrExchange.aspx>

Here is a program that I use to quickly get the images off my flash cards. It's a very simple program; you tell it where the photos are that you want and where you want to put them and it does the rest. It is pretty specific and it does not have much flexibility, so if you happen to like the way it organizes the photos then you'll be happy, but if you want some features added, shoot me an email and I will probably incorporate it.

You can pretty much see from the image below how the photos will be organized in your destination directory. Basically, it creates a "year" folder and a day folder within that. If the file already exists in the destination it will not copy it there. As it copies files it will check to see if it can find image orientation information and will rotate the file accordingly.

I like to use shoot raw together with a small jpeg file. I use the raw as the digital negative and the jpeg as a quick proof of the image. Generally I try to get it right in the camera, and it's also a lot quicker if I just want to take some snapshots and make them quickly available for viewing. Currently it only works with .raw and .jpg extensions, so if you need a different extension let me know and I will add it. I hope you find this useful even though it is a little limited.

Click on the thumbnails below for a closer look...

Download the Program here

NOTE: This is a first draft, alpha code release.

Whenever you are working with Microsoft's SQL server you inevitably have to write some Transact SQL code.

I'm currently working on a data migration script and I found I'm using similar code over and over for the different tables found in the source database.

I've written a small utility that can help in this task. The code connects to the database, collects table information like the columns and column data types, and writes the script.

Below is a list of the current scripts that it creates. I've provided the code for you as well so that you can simply copy and paste into a new console app. You can also download the Visual Studio 2k5 Solution.

Continue reading ‘TableScriptGen – C# 2 Console App’ »

NOTE: This is a first draft, alpha code release.

This application was written to run multiple sql scripts files against an MS Sql server database. I wrote it to learn more about .NET C# 2 Windows application development.

It is pretty much a work in progress, but it’s at a usable point so I decided to release it.

Please download the source below. If you learn from it or find it useful in any way, please leave me a comment. Also, if you enhance it, I’d like a copy.

Screenshots:

Click here to download the C# 2 Source (Visual Studio 2k5) (1.2Mb)

One of my favorite programmers on the net, The Software Jedi, has this cool program called Google Wallpaper. It runs as a systray app, and basically downloads images and sets them as your desktop background based on a predefined list of keywords. It's a great app, but due to recent changes on Google's site, it stopped working.

Here is the broken code in GoogleImageSearch.cs

public string[] DoSearch()
{
    string url = @"http://images.google.com/images?lr=&imgsz=xxlarge&safe="+(_safe?"on":"off")+"&q=" + _keyword;
    WebClient wc = new WebClient();
    string html = wc.DownloadString(url);
    string[] pieces = html.Split(new string[] { "<a href=/imgres?imgurl=" }, StringSplitOptions.None);
    string[] images = new string[pieces.Length-1];
    for (int i = 1; i < pieces.Length; i++) //purposefully starting at 1
    {
        images[i - 1] = pieces[i].Split('&')[0];
    }
    return images;
}

This is the new code...

public string[] DoSearch()
{
    string url = @"http://images.google.com/images?lr=&imgsz=xxlarge&safe=" + (_safe ? "on" : "off") + "&q=" + _keyword;
    WebClient wc = new WebClient();
    string html = wc.DownloadString(url);
    string[] images;

    try
    {
        //Use regular expression to find all the urls to jpg files.
        //Google changed their html, and all image urls are in javascript code.
        Regex RegexObj = new Regex("\\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|](.jpg)", RegexOptions.IgnoreCase);
        MatchCollection RegexMatches = RegexObj.Matches(html);

        images = new string[RegexMatches.Count];

        for (int i = 0; i < RegexMatches.Count; i++)
        {
            // Add each image url string to the string array.
            images[i] = RegexMatches[i].Value;
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }

    return images;
}

Binary + Source (.NET Framework 2.0 Required to run)
Download the .Net 2 Framework here

Please visit The Software Jedi's site for more cool apps!!

I wanted to learn how to use web services in .NET with C#, so I created this tiny program that goes out and gets the "Dilbert of the Day" comic strip and puts it on your desktop in the form of a jpeg file.

There are two versions to this program. One calls the web service synchronously, while the other calls the web services asynchronously. Synchronous calls are what most programmers would be familiar with. When a synchronous function/method is called it doesn't return control back to the calling program until it's finished, while an asynchronous function/method returns control immediately while it runs in a separate thread.

The synchronous version of this program is much simpler than the asynchronous version, but the later allows for more flexibility. For example, in the asynchronous version the program can display a progress indicator while it's waiting for the call to complete.

You could use the synchronous call to the web service and put it in a separate thread yourself. You would achieve the same result as the asynchronous call. It just makes the code a little longer and slightly more complex. I'm not great with threading in C# just yet, so if you want to write this yourself, I'd be glad to post it here and give you credit, of course.

Posted below are both version of the program. You can download the source and the exe separately. Just keep in mind that if you want to run the exe by itself you will need to make sure you have the .NET Framework 2.0 installed.

Any suggestions/comments welcome.

Synchronous Code

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using DilbertADay.com.esynaps.www;

namespace DilbertADay
{
    class Program
    {
        public static void Main(string[] args)
        {
            DailyDilbert wsDd;
            Byte[] imageData;
            MemoryStream strm;
            Bitmap bmp;           

            try
            {
                wsDd = new DailyDilbert();
                imageData = wsDd.DailyDilbertImage();
                strm = new MemoryStream(imageData);
                bmp = new Bitmap(strm);
                bmp.Save(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
                    + "\\dilbert.jpg",ImageFormat.Jpeg);
                bmp.Dispose();
                strm.Close();
                wsDd.Dispose();
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
        }
    }
}

Asynchronous Code

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using DilbertADay.com.esynaps.www;

namespace DilbertADay
{
    class Program
    {
        public static int isCompleted = 0;
        public static Byte[] imgData;
        public static int timeout = 10000;

        public static void Main(string[] args)
        {
            DailyDilbert wsDd;
            MemoryStream strm;
            Bitmap bmp;

            int start, currentSecond;

            try
            {
                wsDd = new DailyDilbert();
                wsDd.DailyDilbertImageCompleted += new DailyDilbertImageCompletedEventHandler(
                    Program.DailyDilbertImageCompleted_Handler);
                wsDd.DailyDilbertImageAsync();

                start = DateTime.Now.Millisecond;
                currentSecond = start;

                Console.WriteLine("Sending request");
                while (Program.isCompleted == 0 && (currentSecond - start) < Program.timeout)
                {
                    if (currentSecond < DateTime.Now.Millisecond)
                    {
                        currentSecond = DateTime.Now.Millisecond;
                        Console.Write(".");
                    }
                }
                Console.Write("\n");

                if (Program.isCompleted == 1)
                {
                    Console.WriteLine("Generating image");
                    strm = new MemoryStream(Program.imgData);
                    bmp = new Bitmap(strm);
                    bmp.Save(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
                        + "\\dilbert.jpg", ImageFormat.Jpeg);

                    bmp.Dispose();
                    strm.Close();
                    wsDd.Dispose();
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
        }

        public static void DailyDilbertImageCompleted_Handler(object sender, DailyDilbertImageCompletedEventArgs args)
        {
            try
            {
                Program.imgData = args.Result;
                Program.isCompleted = 1;
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                Program.isCompleted = 2;
            }
        }
    }
}

Executable (dilbertaday.exe) (24k)
Source Code / VS 2005 Solution (dilbertaday.zip) (81k)