Writing my first C# application…never touched the language before and not much of a programmer! I have been googling around and got a few books but thought the best way to learn is to actually try some coding!
Anyway, been asked to write a little inventory system for all our 15000 servers and I can do this in powershell fine but wanted a challenge and try my hand at C#.
I have the below code and this works fine in a command propmpt window but how to I put this into a winform app? I assume I need to change the “Console.WriteLine” to something else…just I do not know what the something else is! I will probably add in a listbox to show the details in there rather than executing a command prompt..
I will be ading in loads of stuff – like memory info and disk sizes etc so getting this right would help me…plus, no doubt I will ask loads of questions!
Code that works:
using System;
namespace OsVersionSample
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Operating System Detaiils");
OperatingSystem os = Environment.OSVersion;
Console.WriteLine("OS Version: " + os.Version.ToString());
Console.WriteLine("OS Platoform: " + os.Platform.ToString());
Console.WriteLine("OS SP: " + os.ServicePack.ToString());
Console.WriteLine("OS Version String: " + os.VersionString.ToString());
Console.WriteLine();
// Get Version details
Version ver = os.Version;
Console.WriteLine("Major version: " + ver.Major);
Console.WriteLine("Major Revision: " + ver.MajorRevision);
Console.WriteLine("Minor version: " + ver.Minor);
Console.WriteLine("Minor Revision: " + ver.MinorRevision);
Console.WriteLine("Build: " + ver.Build);
Console.ReadLine();
}
}
}
Code I want to put into a winform:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine("Operating System Detaiils");
OperatingSystem os = Environment.OSVersion;
Console.WriteLine("OS Version: " + os.Version.ToString());
Console.WriteLine("OS Platoform: " + os.Platform.ToString());
Console.WriteLine("OS SP: " + os.ServicePack.ToString());
Console.WriteLine("OS Version String: " + os.VersionString.ToString());
Console.WriteLine();
// Get Version details
Version ver = os.Version;
Console.WriteLine("Major version: " + ver.Major);
Console.WriteLine("Major Revision: " + ver.MajorRevision);
Console.WriteLine("Minor version: " + ver.Minor);
Console.WriteLine("Minor Revision: " + ver.MinorRevision);
Console.WriteLine("Build: " + ver.Build);
Console.ReadLine();
}
}
}
Tou can add listbox to form and do something like this