Friend of a Friend (FOAF) in C#


I just want to say up front that I'm not a big fan of formats like this and I'll get into why in a bit but I had to create a simple parser so I decided to share the code. Anyway, FOAF (otherwise known as Friend of a Friend) is a very basic, machine readable markup using RDF/XML to describe people, whom they are friends with, groups they're in, etc. In theory it could be used to allow social networks connect to people outside of their network, connect IM clients, connect... well... anything where a person's basic information is needed. Thankfully you can start using the format immediately:

FOAF.zip (4.15 kb)

And yes, it has the MIT license at the top (which means it's going in my utility library). Anyway, it's commented and it does work for basic items. However, I didn't implement certain items, such as IM client IDs, blog pages, etc. (nor the DNA checksum...) But it does the basic phone number, email address, name, people known, etc. It also works with the FOAF-a-matic.  So if you created a document using that, it will read it.

Now before you get all happy with it and think that you're going to create a social networking site and it will talk to everyone... I've yet to see it used by a large enough groups of sites to make it worth while. You add in the fact that the format is mostly designed for machines only (unlike hCard and XFN), and that there are more widely used concepts like OpenID, and you end up with a format that is OK but you can live without it. I mean Google's Social Graph API works with the format (which is why I created the parser), but it also works with XFN, and XFN can be stylized using CSS, embedded in a web page, etc. And like I said, you have hCards which are gaining momentum, so I just don't see the need yet for FOAF... Never know though as it might catch on more and more as time goes on (I mean I remember a time when people said that XML wasn't going anywhere). Anyway, try out the code, leave feedback, and happy coding.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,
Categories: C# | Web Design
Posted by James Craig on Wednesday, December 03, 2008 10:48 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Killing a Process through C#


I hate testing my code (which is probably fairly evident by the quality on my site)... That being said, I love automating anything and everything. In doing so I needed something that would run an application automatically in the background and kill the application (specifically IE and Firefox as it was some basic web development testing). Finding code that opens up a webpage is rather simple to find so I wont go into that. The idea of killing the process though was a bit more difficult to find (not too much though). And if I had looked under System.Diagnostics I would have found the appropriate object immediately (Process).

The Process class can pretty much handle everything for you. You call one of its functions looking for a specific process, it returns it, you call Kill on the process, and we're done. However I needed this to be done on a timer (to ensure the page loaded completely) and asynchronously (as I needed the application to test multiple things at once and not all of them were going to be web based). Therefore I came up with a couple of functions to help me out... Well actually just two functions:

        /// <summary>
        /// Kills a process after a specified amount of time
        /// </summary>
        /// <param name="ProcessName">Name of the process</param>
        /// <param name="TimeToKill">Amount of time (in ms) until the process is killed.</param>
        public static void KillProcess(string ProcessName, int TimeToKill)
        {
            ThreadPool.QueueUserWorkItem(delegate { KillProcessAsync(ProcessName, TimeToKill); });
        }

        /// <summary>
        /// Kills a process asyncronously
        /// </summary>
        /// <param name="ProcessName">Name of the process to kill</param>
        /// <param name="TimeToKill">Amount of time until the process is killed</param>
        private static void KillProcessAsync(string ProcessName, int TimeToKill)
        {
            if(TimeToKill>0)
                Thread.Sleep(TimeToKill);
            Process[] Processes = Process.GetProcessesByName(ProcessName);
            foreach (Process CurrentProcess in Processes)
            {
                    CurrentProcess.Kill();
            }
        }

The first function simply calls the second function and puts it into its own thread. The second process on the other hand actually asks for the processes (after sleeping the appropriate amount of time) and kills them. It's very basic and commented so I don't believe it would take too much time to understand. But with this, I've been able to set my computer on auto pilot at night, gathering info for me to go over in the morning and ensure that I come back to a clean desktop. So I hope this helps you out as well. So try it out, leave feedback, and happy coding.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: C#
Posted by James Craig on Monday, December 01, 2008 1:46 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Using Reflection to get Assembly Information in C#


I've been working on improving my debug/error reporting capabilities (which includes unit testing, automation, etc.). One of the items on that list though, is what I actually record. Up until now that has included system information (OS, memory, etc.), basic profiling, error message, etc. However one of the items that would have been helpful and I simply wasn't recording was information about what assemblies were currently loaded by the system. It's usually easy to figure out (just look at the references list), but sometimes a few get in there that you aren't expecting. So to help out, I created a couple functions to find out this information:


        public static string DumpAllAssembliesAndProperties()
        {
            StringBuilder Builder = new StringBuilder();
            Assembly[] Assemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach (Assembly Assembly in Assemblies)
            {
                Builder.Append("<strong>" + Assembly.GetName().Name + "</strong><br />");
                Builder.Append(DumpProperties(Assembly)+"<br /><br />");
            }
            return Builder.ToString();
        }

        public static string DumpProperties(object Object)
        {
            try
            {
                StringBuilder TempValue = new StringBuilder();
                TempValue.Append("<table><thead><tr><th>Property Name</th><th>Property Value</th></tr></thead><tbody>");
                Type ObjectType = Object.GetType();
                PropertyInfo[] Properties = ObjectType.GetProperties();
                foreach (PropertyInfo Property in Properties)
                {
                    TempValue.Append("<tr><td>" + Property.Name + "</td><td>");
                    if (Property.GetIndexParameters().Length == 0)
                    {
                        try
                        {
                            TempValue.Append(Property.GetValue(Object, null) == null ? "null" : Property.GetValue(Object, null).ToString());
                        }
                        catch { }
                    }
                    TempValue.Append("</td></tr>");
                }
                TempValue.Append("</tbody></table>");
                return TempValue.ToString();
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

The above function DumpAllAssembliesAndProperties is the one you want to call. It in turn will figure out what assemblies are loaded and output (in an HTML formatted string) the name as well as property information (file name, etc.). It's pretty basic but thanks to the function I was able to find a couple dependencies and remove them and in one case discovered I was using the wrong version. Also note that the DumpProperties function will allow you to take any object and dump the properties of it along with their current values (another useful function to have around when debugging). Anyway, I hope this helps you out. So try out the code, leave feedback, and happy coding.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: C#
Posted by James Craig on Wednesday, November 26, 2008 2:27 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Recent comments

None

Calendar

<<  December 2008  >>
SuMoTuWeThFrSa
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Sponsors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008