How to call a basic C# DLL


  • I am experimenting with a few ways of doing things and have seen that if I have routines that are used in multiple projects, then I can create that code in a DLL & call it from those different projects thereby removing the need for repeating code.

    I've started off by just creating a small program that will tell me what version of Windows I am running, but in this case I don't need to pass anything to the DLL, I just want to call it and read the results ... how do I do this ?!?

    This is my code and I think it will work, but I have written it to accept data & obviously I don't actually need to supply any parameters, what do I need to do ? Sorry, I am new to C# and am having real trouble getting my head around the terminology, let alone the logic !!!

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace MyWindowsVersion
        public class WindowsVersion
            public static string myNull(string myOSver)
                String myStr = Environment.OSVersion.Version.ToString();
                int myOSver1 = int.Parse(myStr.Substring(0,1));
                int myOSver3 = int.Parse(myStr.Substring(0,3));
                if (myOSver1 < 5)
                    myOSver = "Windows version prior to XP";
                else if (myOSver3 < 5.1)
                    myOSver = "Windows version prior to XP";
                else if (myOSver3 < 6.0)
                    myOSver = "Windows XP";
                else if (myOSver3 < 6.1)
                    myOSver = "Windows Vista";
                else if (myOSver3 == 6.1)
                    myOSver = "Windows 7";
                else myOSver = "Windows Unknown Version";
                return (myOSver);

    As you can see, I don't know how to write it so that it isn't expecting a parameter, so I've assigned the string to be returned in the "Public Static" line, is that the way around this ?

    However, I am having trouble picking it up in the main program, I'm obviously doing something wrong, but I've tried to do the same as the MSDN example at but when I code ...

    String myWinVer = MyWindowsVersion.WindowsVersion("x");

    ... I get an error "'MyWindowsVersion.WindowsVersion' is a 'type', which is not valid in the given context" ... What does this mean ?!?!?

    Thanks in advance for your help ...

    miércoles, 04 de abril de 2012 19:55


  • Perhaps you meant:

    string myWinVer = MyWindowsVersion.WindowsVersion.myNull("x");

    if you don't want to have to pass in the dummy string then change:

    public static string myNull(string myOSver)


    public static string myNull()
    and change the internal references to myOSver to something that has local scope.

    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    miércoles, 04 de abril de 2012 20:25