neomatic
Goto Top

CSharp - Zeile formatiert in Textdatei speichern

Ich möchte in C# eine Textzeile formatiert speichern.

Hallo Leute,

ich möchte in C# eine Textzeile formatiert in eine Textdatei speichern. Folgender Code verwende ich:

            ManagementObjectSearcher svc = new ManagementObjectSearcher("root\\CIMV2","SELECT * FROM Win32_PerfFormattedData_PerfProc_Process");  
            
            foreach (ManagementObject queryObj in svc.Get())
            {
                int process_memory = Convert.ToInt32(queryObj["WorkingSetPrivate"]);  

                ticketWriter.WriteLine("{0}\t\t\t{1}\t\t\t{2}", queryObj["Name"], queryObj["PercentProcessorTime"] + " %", process_memory / 1024 + " kb");  
            }

Das Ergebnis sieht so aus:

taskhost#1			0 %			3904 kb
firefox			0 %			384020 kb
plugin-container			0 %			56856 kb
plugin-container#1			0 %			5184 kb
wuauclt			0 %			1624 kb
winamp			0 %			11244 kb


Nun möchte ich aber, dass das Ergebnis so aussieht:

taskhost#1			0 %			3904 kb
firefox			        0 %			384020 kb
plugin-container		0 %			56856 kb
plugin-container#1		0 %			5184 kb
wuauclt			        0 %			1624 kb
winamp			        0 %			11244 kb

Jedoch komme ich mit Tabstops nicht weiter. Gibt es eine möglichkeit das ganze richtig zu speichern?

Gruß

Neomatic

Content-Key: 171304

Url: https://administrator.de/contentid/171304

Printed on: April 19, 2024 at 20:04 o'clock

Member: mathe172
mathe172 Aug 11, 2011 at 15:31:48 (UTC)
Goto Top
Hallo,

eine Lösung wäre es, wenn du die Spaltentexte (z.B. "taskhost#1", "0 %" oder "3904 kb") mit String.PadLeft() auf eine Länge von beispielsweise 20 Zeichen bringst und diese Teile zusammensetzt.

MfG,
Mathe172
Member: Neomatic
Neomatic Aug 11, 2011 at 21:53:43 (UTC)
Goto Top
Hallo,

Ich habe es hinbekommen. face-smile


Ich musste die Zeile folgendermaßen abändern:

ticketWriter.WriteLine(String.Format("{0,-25} {1,5} {2,20}", queryObj["Name"], queryObj["PercentProcessorTime"] + " %", process_memory / 1024 + " kB"));  

Vielen Dank für den Tip.

Gruß

Neomatic