My SystemHelper class

I've posted a few code snippets and some people noticed that there are references to unresolved methods.I am using a few helper classes. This post describes my SystemHelper class:

 

namespace FSHelperLib

{

    using Iesi.Collections;

    using Microsoft.VisualBasic;

    using Microsoft.VisualBasic.CompilerServices;

    using Microsoft.Win32;

    using System;

      using System.IO;

    using System.Collections;

      using System.Diagnostics ;

      using System.Reflection;

    using System.Runtime.CompilerServices;

    using System.Web;

 

    public class SystemHelper

    {

        // Methods

        public SystemHelper()

        {

        }

 

        public static string AppTitle()

        { //       'From http://www.dotnet247.com/247reference/msgs/15/78041.aspx

            if (SystemHelper.IsWebApplication())

            {

                char[] chArray1 = new char[1] { '/' } ;

                return HttpContext.Current.Request.ApplicationPath.TrimStart(chArray1); //'returns name like "/vLearning"

            }

//                'or sProductName = Process.GetCurrentProcess().ProcessName

//            ' or Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName)

            Assembly asm = Assembly.GetEntryAssembly();

                  if(asm==null) //if called from NUnit Debugger

                  {asm = Assembly.GetExecutingAssembly();

                  }

            return AssemblyTitle( asm);

        }

            public static string AssemblyTitle(Assembly asm)

            { //       'From http://www.dotnet247.com/247reference/msgs/15/78041.aspx

                  AssemblyTitleAttribute attribute1 = (AssemblyTitleAttribute) Attribute.GetCustomAttribute(asm, typeof(AssemblyTitleAttribute));

                  //        'Application.ProductName is not working in Dll

                  //        'Assembly.GetEntryAssembly().FullName not reliable ?? see http://weblogs.asp.net/asanto/posts/26710.aspx

                  string sRet=attribute1.Title;

                  if (DataHelper.IsNullOrEmpty( sRet))

                  {sRet= asm.GetName().Name;

                  }

                  return sRet ;

            }

            ///

            /// Reads from table, decrypts and check  as valid date and integers

            ///

            /// Assembly.GetExecutingAssembly()

            ///

            public static string GetAssemblyVersion(Assembly asm)

            {

                  FileVersionInfo info1 = FileVersionInfo.GetVersionInfo(asm.Location);

                  return info1.ProductVersion;

            }

 

 

        public static bool IsWebApplication()

        { //'http://groups.google.com.au/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=ebVausWRBHA.1408%40tkmsftngp05

                  //issue: it will return wrong answer if called from async thread running from Web

                  //try to check some System.AppDomain.CurrentDomain properties

            bool bIsASP = true;

            if (Information.IsNothing(HttpContext.Current))

            {

                bIsASP = false;

            }

            return bIsASP;

        }

            public static string GetWebRootFolder(Assembly ExecutableAssembly)

            {

                  //Debug.Assert(IsWebApplication()); can be called frm installer or tester

                  string sPath = ExecutableAssembly.Location;

                  //remove assembly name

                  sPath = sPath.Remove(sPath.LastIndexOf(@"\"), Strings.Len(sPath) - sPath.LastIndexOf(@"\"));

                  //remove BIN folder name

            if((sPath.ToUpper().EndsWith(@"\BIN")) )//15/6/2006

            {

                sPath = sPath.Remove(sPath.LastIndexOf(@"\"), Strings.Len(sPath) - sPath.LastIndexOf(@"\"));

            }

                  Debug.Assert(Directory.Exists(sPath));

                  //Debug.Assert(sPath==System.AppDomain.CurrentDomain.BaseDirectory);//can be called frm installer or tester

                  return sPath;

            }

 

            //'Addition to math.Max

            public static object Max(Array elements)

        {

            object oRet=null;

            SortedSet set1 = new SortedSet(elements);

            IEnumerator enumerator1 = set1.GetEnumerator();

            while (enumerator1.MoveNext())

            {

                oRet = RuntimeHelpers.GetObjectValue(enumerator1.Current);

            }

            return oRet;

        }

            ///

            /// Creates a COM object given it's ProgID.

            ///

            /// The ProgID to create

            /// The newly created object, or null on failure.

            //from http://www.novicksoftware.com/TipsAndTricks/tip-csharp-create-com-object-by-progid.htm

            public static object COMCreateObject (string sProgID)

            {

                  // We get the type using just the ProgID

                  Type oType = Type.GetTypeFromProgID (sProgID);

                  if (oType != null)

                  {                            

                        return Activator.CreateInstance(oType);

                  }

                 

                  return null;

            }

            public static void LateSet(         object o,               Type objType,                 string name,      string Value)

            {

                  object[] objArray1 = new object[1] { Value } ;

                  LateBinding.LateSet(o , null, name, objArray1, null);

            }

 

 

            //'Addition to math.Min

        public static object Min(Array elements)

        {

            object oRet=null;

            SortedSet set1 = new SortedSet(elements);

            IEnumerator enumerator1 = set1.GetEnumerator();

            while (enumerator1.MoveNext())

            {

                oRet = RuntimeHelpers.GetObjectValue(enumerator1.Current);

                break;

            }

            return oRet;

        }

 //See DateTime.Compare

        public static DateTime Min(DateTime t1, DateTime t2)

        {

            if (DateTime.Compare(t1, t2) > 0)

            {

                return t2;

            }

            return t1;

        }

 

 

     }

}

 

 

posted @ Monday, August 14, 2006 9:10 AM

Print

Comments on this entry:

# re: My SystemHelper class

Left by Mufasa at 2/15/2008 3:55 AM
Gravatar
I think your Min() function has an error. Should you be returning t1 on the last line of the method?

# re: My SystemHelper class

Left by Michael Freidgeim at 2/15/2008 8:57 AM
Gravatar
Yes, it had. Thank you for pointing to it.I've updated the post.

Your comment:



 (will not be displayed)


 
 
 
Please add 6 and 3 and type the answer here:
 

Live Comment Preview:

 
«December»
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910