From b292b8f679792ea9db0e4a75eb99d8fcb9983528 Mon Sep 17 00:00:00 2001 From: Creepycrafter Playername Date: Tue, 26 Feb 2019 15:51:57 +0100 Subject: [PATCH] Stuffs --- WinPart/Program.cs | 121 ++++++++++++++++++++++++--------------------- 1 file changed, 64 insertions(+), 57 deletions(-) diff --git a/WinPart/Program.cs b/WinPart/Program.cs index e04ba70..eb9e28a 100644 --- a/WinPart/Program.cs +++ b/WinPart/Program.cs @@ -4,17 +4,19 @@ using System.Diagnostics; using System.Management; using CommandLine.Utility; using WinPart.Properties; +using System.Collections; #pragma warning disable IDE1006 namespace WinPart { class Program { List drives; - bool longInputSupport; + bool longInputSupport, debug; static void Main(string[] args) => new Program().main(new Arguments(args)); void main(Arguments args) { longInputSupport = args["li"] != null; + debug = args["debug"] != null; bool run = true; - if (args["help"] != null) { - Console.WriteLine("WinPart is a tool for manipulating storage.\r\n\r\nWinPart [-li] [-help]\r\n\r\nli Enables large inputs (over 1 char)\r\nhelp Shows this message"); + if ((args["help"] != null) || (args["?"] != null)) { + Console.WriteLine("WinPart is a tool for manipulating storage.\r\n\r\nWinPart [-li] [-help] [-debug]\r\n\r\nli Enables large inputs (over 1 char)\r\nhelp Shows this message\r\ndebug Troubleshooting"); run = false; } if (run) @@ -22,7 +24,6 @@ namespace WinPart { } void mainWindow() { - Console.Clear(); Console.WriteLine("Use \"WinPart -help\" for CMD Args"); Console.WriteLine("Welcome to WinPart. Choose one of the disks by their number to continue."); try { @@ -30,13 +31,12 @@ namespace WinPart { int i = 0; foreach (ManagementObject mo in new ManagementObjectSearcher("select * from Win32_DiskDrive").Get()) { i++; - drives.Add(new Device().getFromMO(mo)); + drives.Add(new Device().getFromMO(mo, this)); Console.WriteLine(i.ToString() + ": " + drives[drives.Count - 1].name); } Device drive = drives[int.Parse(getInput()) - 1]; Console.WriteLine("Choose one of the operations by their number to continue.\r\n1: Info\r\n2: Open"); - switch (int.Parse(getInput())) - { + switch (int.Parse(getInput())) { case 1: drive.getInfo(); break; @@ -46,8 +46,7 @@ namespace WinPart { Console.WriteLine((i + 1).ToString() + ": " + drive.partitions[i].name); Partition partition = drive.partitions[int.Parse(getInput()) - 1]; Console.WriteLine("Choose one of the operations by their number to continue.\r\n1: Info\r\n2: Open"); - switch (int.Parse(getInput())) - { + switch (int.Parse(getInput())) { case 1: partition.getInfo(); break; @@ -57,8 +56,7 @@ namespace WinPart { Console.WriteLine((i + 1).ToString() + ": " + partition.logicalDrives[i].name); LogicalDrive logicalDrive = partition.logicalDrives[int.Parse(getInput()) - 1]; Console.WriteLine("Choose one of the operations by their number to continue.\r\n1: Info\r\n2: Explorer"); - switch (int.Parse(getInput())) - { + switch (int.Parse(getInput())) { case 1: logicalDrive.getInfo(); break; @@ -79,6 +77,7 @@ namespace WinPart { break; } } catch (Exception e) { Console.WriteLine(e.ToString()); } + if (debug) Console.ReadKey(); } string getInput() { @@ -91,78 +90,86 @@ namespace WinPart { Console.Write("\r\n\r\n"); return out_; } + + public void printInfo(ManagementObject mo, string s) { + string[] tmp1 = { }; + PropertyData tmp2; + try { + tmp1 = s.Split(" ".ToCharArray()); + if (tmp1[tmp1.Length - 1].EndsWith("[]")) { + tmp2 = mo.Properties[tmp1[tmp1.Length - 1].Replace("[]", "")]; + string tmp3 = "[ "; + object[] list = (object[])tmp2.Value; + for (int i = 0; i < list.Length; i++) { + tmp3 += list[i].ToString(); + if (i + 1 < list.Length) tmp3 += " | "; + } + tmp3 += " ]"; + Console.WriteLine(tmp2.Name + ": " + tmp3); + } else { + tmp2 = mo.Properties[tmp1[tmp1.Length - 1]]; + Console.WriteLine(tmp2.Name + ": " + tmp2.Value); + } + } catch (Exception e) { Console.Write("Could not read: " + tmp1[tmp1.Length - 1]); if (debug) Console.Write(" due to: " + e.ToString()); Console.Write("\r\n"); } + } } class Device { + public Program program; public List partitions; public string name; public ManagementObject mo; - public Device getFromMO(ManagementObject MO) { + public Device getFromMO(ManagementObject MO, Program program_) { + mo = MO; + program = program_; partitions = new List() { }; foreach (ManagementObject m in new ManagementObjectSearcher(string.Format("associators of {{{0}}} where AssocClass = Win32_DiskDriveToDiskPartition", MO.Path.RelativePath)).Get()) - partitions.Add(new Partition().getFromMO(m)); - name = (string)MO.Properties["DeviceId"].Value; - mo = MO; + partitions.Add(new Partition().getFromMO(m, program)); + name = (string)MO.Properties["Model"].Value; return this; } public void getInfo() { - Console.WriteLine("PhysicalName: " + Convert.ToString(mo.Properties["Name"].Value)); - Console.WriteLine("DiskName: " + Convert.ToString(mo.Properties["Caption"].Value)); - Console.WriteLine("DiskModel: " + Convert.ToString(mo.Properties["Model"].Value)); - Console.WriteLine("DiskInterface: " + Convert.ToString(mo.Properties["InterfaceType"].Value)); - Console.WriteLine("Capabilities: " + (ushort[])mo.Properties["Capabilities"].Value); - Console.WriteLine("MediaLoaded: " + Convert.ToBoolean(mo.Properties["MediaLoaded"].Value)); - Console.WriteLine("MediaType: " + Convert.ToString(mo.Properties["MediaType"].Value)); - Console.WriteLine("MediaSignature: " + Convert.ToUInt32(mo.Properties["Signature"].Value)); - Console.WriteLine("MediaStatus: " + Convert.ToString(mo.Properties["Status"].Value)); + foreach (string s in Resources.Device.Split(new[] { "\r\n" }, StringSplitOptions.None)) + program.printInfo(mo, s); } } class Partition { + public Program program; public List logicalDrives; public string name; public ManagementObject mo; - public Partition getFromMO(ManagementObject MO) { + public Partition getFromMO(ManagementObject MO, Program program_) { + mo = MO; + program = program_; logicalDrives = new List() { }; foreach (ManagementObject m in new ManagementObjectSearcher(string.Format("associators of {{{0}}} where AssocClass = Win32_LogicalDiskToPartition", MO.Path.RelativePath)).Get()) - logicalDrives.Add(new LogicalDrive().getFromMO(m)); + logicalDrives.Add(new LogicalDrive().getFromMO(m, program)); name = MO.ToString(); - mo = MO; - return this; - } - - public void getInfo() - { - foreach (string s in Resources.Partition.Split("\r\n".ToCharArray())) - { - var tmp1 = s.Split(" ".ToCharArray()); - var tmp2 = mo.Properties[tmp1[tmp1.Length]]; - Console.WriteLine(tmp2.Name + tmp2.Value); - } - } - } - - class LogicalDrive { - public string name; - public ManagementObject mo; - public LogicalDrive getFromMO(ManagementObject MO) { - name = (string)MO.Properties["Name"].Value; - mo = MO; return this; } public void getInfo() { - Console.WriteLine("DriveName: " + mo.Properties["Name"].Value); - Console.WriteLine("DriveId: " + mo.Properties["DeviceId"].Value); - Console.WriteLine("DriveCompressed: " + mo.Properties["Compressed"].Value); - Console.WriteLine("DriveType: " + mo.Properties["DriveType"].Value); - Console.WriteLine("FileSystem: " + mo.Properties["FileSystem"].Value); - Console.WriteLine("FreeSpace: " + mo.Properties["FreeSpace"].Value); - Console.WriteLine("TotalSpace: " + mo.Properties["Size"].Value); - Console.WriteLine("DriveMediaType: " + mo.Properties["MediaType"].Value); - Console.WriteLine("VolumeName: " + mo.Properties["VolumeName"].Value); - Console.WriteLine("VolumeSerial: " + mo.Properties["VolumeSerialNumber"].Value); + foreach (string s in Resources.Partition.Split(new[] { "\r\n" }, StringSplitOptions.None)) + program.printInfo(mo, s); + } + } + + class LogicalDrive { + public Program program; + public string name; + public ManagementObject mo; + public LogicalDrive getFromMO(ManagementObject MO, Program program_) { + name = (string)MO.Properties["Name"].Value; + mo = MO; + program = program_; + return this; + } + + public void getInfo() { + foreach (string s in Resources.Logical.Split(new[] { "\r\n" }, StringSplitOptions.None)) + program.printInfo(mo, s); } } }