Click here to Skip to main content
15,881,380 members
Articles / Programming Languages / XML

Using InsomniaServer to Build a Web-interface for your Application

Rate me:
Please Sign up or sign in to vote.
4.80/5 (11 votes)
15 Jul 2011CPOL3 min read 83K   1.2K   41  
InsomniaServer enables you to add a fully-featured, customizable webserver to your projects. See how it works.
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

using InsomniaSoftware.Server;
using InsomniaSoftware.Server.Sources;
using InsomniaSoftware.Server.Tools;

namespace SystemBrowser
{
    class SystemInformation
    {
        static string pageHead = "<html><head><title>System information - query window</title><style type=\"text/css\"><!--.spacer { position: relative; left: 30px; } .border { border: solid 2px gray; }--></style></head>\n<body>";

        static KeyValuePair<string, string>[] sysInfoClasses = { new KeyValuePair<string, string>("BIOS", "Win32_BIOS"),
                                                                 new KeyValuePair<string, string>("Processor", "Win32_Processor"),
                                                                 new KeyValuePair<string, string>("Memory", "Win32_PhysicalMemory"),
                                                                 new KeyValuePair<string, string>("Logical disks", "Win32_LogicalDisk"),
                                                                 new KeyValuePair<string, string>("Video controller", "Win32_VideoController"),
                                                                 new KeyValuePair<string, string>("Network adapters", "Win32_NetworkAdapter") };

        public static ObjectType GetSystemInformation(string subPath, HttpRequest request, object userData, out Source source)
        {
            source = null;
            Session sess = null;
            try
            {
                //Load user's session
                sess = request.server.sessionManager.GetSession(request.GetParameter("sid").ToString());
            }
            catch { }

            if (!((Login.User)sess.GetValue("user")).systemInformationAllowed)
            {
                //User is not allowed to use this feature
                source = FileSource.Get("../pages/accessnotpermitted.html");
                return ObjectType.File;
            }

            string query = "";
            HttpParameter queryParam = request.GetParameter("query");
            if(queryParam != null)
                query = queryParam.ToString();

            string result = "";
            if (query.Length > 0)
            {
                result += "<b>Query:</b> " + query + "<br><br>";
                try
                {
                    WqlObjectQuery wqlQuery = new WqlObjectQuery(query);
                    ManagementObjectSearcher searcher = new ManagementObjectSearcher(wqlQuery);
                    foreach (ManagementObject disk in searcher.Get())
                    {
                        result += "<div class=\"border\">";
                        try
                        {
                            result += "<b>" + disk.ToString() + "</b><br>\n<div class=\"spacer\">";
                            foreach (PropertyData curData in disk.Properties)
                            {
                                try
                                {
                                    result += "<i>" + HttpTools.EncodeHtmlString(curData.Name) + ":</i> " + (curData.Value != null ? HttpTools.EncodeHtmlString(curData.Value.ToString()) : null) + "<br>\n";
                                }
                                catch { }
                            }
                        }
                        catch { }
                        result += "</div></div>";
                    }
                }
                catch(Exception e)
                {
                    result += "Query exception: " + e.Message + " @ " + e.StackTrace;
                }
                result += "<br><br>";
            }

            string pageBottom = "<b>Predefined queries:</b><ul>";
            foreach(KeyValuePair<string, string> curPair in sysInfoClasses)
            {
                string linkQueryStr = "SELECT * FROM " + curPair.Value;
                pageBottom += "<li><a href=\"" + request.requestedURL + "?sid=" + sess.id + "&query=" + HttpTools.EncodeHttpString(linkQueryStr, Encoding.UTF8) + "\">" + curPair.Key + "</a></li>";
            }
            pageBottom += "</ul><br><br><form action=\"" + request.requestedURL + "\" method=\"get\"><b>User-defined WQL query:</b><br><br><input type=\"hidden\" name=\"sid\" value=\"" + sess.id + "\"><input type=\"text\" name=\"query\" size=\"80\"><br><input type=\"submit\" value=\"Start query!\"></form></body></html>";

            source = new BinarySource(ASCIIEncoding.ASCII.GetBytes(pageHead + result + pageBottom), ContentType.FromType(ContentType.Type.Html));
            return ObjectType.File;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
Germany Germany
I was born in 1987. Unfortunately too late to experience the real rise of the PC. But fortunately late enough to enjoy things like MS's .net during my school time Wink | ;)

From the time when some relative taught me a little BASIC under MS DOS, I loved to tell computers what to do - even though my real start in programming was around the age of 16.

At the moment, I am studying Software Engineering at University of Augsburg, always hoping to find time to design and program.
Besides, I like meeting friends, spent time with my girlfriend and enjoy life Smile | :)

Comments and Discussions