Hab gerade erst angefangen und meine ersten 3 Konsolenanwendungen geschrieben, also verzeiht mir wenn ich an manchen stellen etwas unelegant vorgegangen bin. Wenn jemand eines dieser Programme kompiliert haben will, kann ich auch noch die jeweilige exe hochladen.
1.Programm: Rechner
- Code: Alles auswählen
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static string operation = "";
static void Main(string[] args)
{
Console.WriteLine("Geben sie x zum Beenden ein ");
while (operation != "x")
{
Console.WriteLine("Geben sie die Operation an (+ - * /)");
operation = Console.ReadLine();
if (operation == "+" || operation == "-" || operation == "*" || operation == "/" || operation == "x")
{
//Zum addieren
if (operation == "+")
{
Console.Write("Wert1: ");
string stringWert1 = Console.ReadLine();
Console.Write("Wert2: ");
string stringWert2 = Console.ReadLine();
double intWert1 = double.Parse(stringWert1);
double intWert2 = double.Parse(stringWert2);
double ergebnis = intWert1 + intWert2;
Console.Write("Ergebnis: ");
Console.WriteLine(ergebnis);
}
//Zum subtrahieren
if (operation == "-")
{
Console.Write("Wert1: ");
string stringWert1 = Console.ReadLine();
Console.Write("Wert2: ");
string stringWert2 = Console.ReadLine();
double intWert1 = double.Parse(stringWert1);
double intWert2 = double.Parse(stringWert2);
double ergebnis = intWert1 - intWert2;
Console.Write("Ergebnis: ");
Console.WriteLine(ergebnis);
}
//Zum multiplizieren
if (operation == "*")
{
Console.Write("Wert1: ");
string stringWert1 = Console.ReadLine();
Console.Write("Wert2: ");
string stringWert2 = Console.ReadLine();
double intWert1 = double.Parse(stringWert1);
double intWert2 = double.Parse(stringWert2);
double ergebnis = intWert1 * intWert2;
Console.Write("Ergebnis: ");
Console.WriteLine(ergebnis);
}
//Zum dividieren
if (operation == "/")
{
Console.Write("Wert1: ");
string stringWert1 = Console.ReadLine();
Console.Write("Wert2: ");
string stringWert2 = Console.ReadLine();
double intWert1 = double.Parse(stringWert1);
double intWert2 = double.Parse(stringWert2);
if (intWert2 == 0)
{
Console.WriteLine("Falsche Eingabe");
}
else
{
double ergebnis = intWert1 / intWert2;
Console.Write("Ergebnis: ");
Console.WriteLine(ergebnis);
}
}
}
else
{
Console.WriteLine("Falsche Eingabe");
}
}
}
}
}
2.Programm: Fährt bei falscher Eingabe den PC herunter
- Code: Alles auswählen
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static string name = "";
static void Main(string[] args)
{
Console.Write("Geben sie ihren Namen ein: ");
name = Console.ReadLine();
if (name == "Dani" || name == "Daniel" || name == "dani" || name == "daniel")
{
Console.WriteLine("Richtige Eingabe");
Console.ReadKey();
}
else
{
Console.WriteLine("Falsche Eingabe!");
Console.WriteLine("Pc wird heruntergefahren!");
writeFile();
openFile();
Console.ReadKey();
}
}
private static void writeFile()
{
StreamWriter writer = new StreamWriter("C:\\Shutdown.bat", false);
writer.WriteLine("shutdown -s -t 10");
writer.Close();
}
private static void openFile()
{
Process.Start("C:\\Shutdown.bat");
}
}
}
3.Programm: Müllt die C Partition mit 5 riesigen Textdateien voll oder öffnet 500 mal den Internetexplorer
(Ok dieses Programm ist wirklich der Gipfel der Sinnlosigkeit, aber ich hatte Langeweile und es gibt auch eine Warnung am Anfang
- Code: Alles auswählen
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Management;
namespace ConsoleApplication1
{
class Program
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private const int SW_MINIMIZE = 6;
private const int SW_MAXIMIZE = 3;
private const int SW_RESTORE = 9;
static void Main(string[] args)
{
double diskSpace = 0;
double diskSpace2 = 0;
double diskSpace3 = testSpace();
double restSpace = 0;
Console.Write("(WARNUNG!) Wollen sie dieses Programm wirklich ausführen? (J/N): ");
string antwort = Console.ReadLine();
if (antwort == "j" || antwort == "J")
{
Console.WriteLine("Bitte warten, dieser Vorgang kann einige Minuten in Anspruch nehmen");
diskSpace = testSpace();
string activeDir = @"c:\\";
string newPath = System.IO.Path.Combine(activeDir, "junk");
System.IO.Directory.CreateDirectory(newPath);
while (diskSpace3 > 4000000000)
{
for (int i = 0; i < 5; i++)
{
writeFile(i);
}
diskSpace3 = testSpace();
}
diskSpace2 = testSpace();
restSpace = diskSpace - diskSpace2;
restSpace = restSpace / 1073741824;
Math.Round(restSpace);
Console.Write("Sie haben nun ");
Console.Write(restSpace.ToString());
Console.WriteLine(" GigaByte weniger Speicherplatz auf ihrer C Partition");
Console.WriteLine("");
Console.WriteLine("Zum Beenden Beliebige Taste drücken");
Console.ReadKey();
}
else if(antwort == "n" || antwort == "N")
{
Console.WriteLine("Schade :(");
Thread.Sleep(2000);
}
else
{
Console.WriteLine("Lern Schreiben!");
Thread.Sleep(2000);
IntPtr winHandle =
System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
ShowWindow(winHandle, SW_MINIMIZE);
Process.Start("IExplore.exe", "www.youtube.com/watch?v=hnyy0dXrzkM");
for (int e = 0; e <= 500; e++)
{
Process.Start("IExplore.exe", "www.myspaceantics.com/images/myspace-graphics/funny-pictures/owned-subaru.JPG");
}
}
}
static void writeFile(int i)
{
String filnam = "C:\\junk\\junk";
filnam += i.ToString() + ".txt";
StreamWriter writer = new StreamWriter(filnam, true);
for (int j = 0; j <= 10000000; j++)
{
writer.WriteLine("junkjunkjunkjunkjunkjunkjunkjunkjunk");
}
writer.Close();
}
static double testSpace()
{
double diskSpace = 0;
ManagementObjectSearcher query;
ManagementObjectCollection queryCollection;
System.Management.ObjectQuery oq;
string stringMachineName = "localhost";
ConnectionOptions co = new ConnectionOptions();
System.Management.ManagementScope ms = new System.Management.ManagementScope("\\\\" + stringMachineName + "\\root\\cimv2", co);
oq = new System.Management.ObjectQuery("SELECT * FROM Win32_LogicalDisk WHERE DeviceID = 'C:'");
query = new ManagementObjectSearcher(ms, oq);
queryCollection = query.Get();
foreach (ManagementObject mo in queryCollection)
{
diskSpace = double.Parse(mo["FreeSpace"].ToString());
}
return diskSpace;
}
}
}
