如何在C# WinForms的ListView中显示本地txt分区文件数据?
Hey there! Let me walk you through exactly how to display your partition.txt data in a WinForms ListView to match that polished look you mentioned. Here's a step-by-step implementation that’s easy to follow:
1. Set Up the ListView Control First
First, get your ListView configured to show structured, table-like data. You can do this via the designer or code—both work great:
Option A: Using the Designer
- Drag a ListView onto your form.
- In the Properties window:
- Set
Viewto Details (this enables column-based display) - Check
FullRowSelect(so clicking a row selects all columns at once) - Check
GridLines(adds clean borders between rows/columns, like your reference image) - Add columns matching your file’s headers: Number, Start, End, Size, File system, Name, Flags. Tweak each column’s
Widthfor readability.
- Set
Option B: Using Code (Great for Dynamic Setup)
Add this to your form’s Load event handler to configure the ListView programmatically:
private void Form1_Load(object sender, EventArgs e) { // Core ListView settings listView1.View = View.Details; listView1.FullRowSelect = true; listView1.GridLines = true; listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize); // Add columns matching your partition data listView1.Columns.Add("Number", 70); listView1.Columns.Add("Start", 120); listView1.Columns.Add("End", 120); listView1.Columns.Add("Size", 100); listView1.Columns.Add("File system", 120); listView1.Columns.Add("Name", 150); listView1.Columns.Add("Flags", 100); }
2. Read and Parse the Text File
Next, we’ll read the file, skip the header line, and convert each data row into a ListView item. I’m assuming your file uses tab-separated values (super common for system info like this)—if it uses commas or spaces instead, just adjust the split character in the code.
Add this method to your form:
private void LoadPartitionData() { string filePath = @"C:\users\Dell\partition.txt"; try { // Read all lines from the file (safe for 10 rows) string[] lines = File.ReadAllLines(filePath, Encoding.Default); // Skip the first header line and process data rows for (int i = 1; i < lines.Length; i++) { string line = lines[i].Trim(); if (string.IsNullOrEmpty(line)) continue; // Skip empty lines // Split the line into columns (adjust delimiter if needed) string[] columns = line.Split('\t'); // Use ',' for commas, ' ' for spaces // Only add if we have all 7 expected columns if (columns.Length == 7) { // Create a ListView item for the partition ListViewItem item = new ListViewItem(columns[0]); // Number column item.SubItems.Add(columns[1]); // Start item.SubItems.Add(columns[2]); // End item.SubItems.Add(columns[3]); // Size item.SubItems.Add(columns[4]); // File system item.SubItems.Add(columns[5]); // Name item.SubItems.Add(columns[6]); // Flags // Add the item to the ListView listView1.Items.Add(item); } } // Resize columns to fit content after loading listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent); } catch (FileNotFoundException) { MessageBox.Show("Couldn't find partition.txt at the specified path.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { MessageBox.Show($"Oops, something went wrong: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
3. Trigger the Load
Finally, call the LoadPartitionData() method from your form’s Load event, right after configuring the ListView:
private void Form1_Load(object sender, EventArgs e) { // ... (previous ListView setup code) LoadPartitionData(); }
Quick Adjustment Tips
- If your file uses fixed-width columns (instead of delimiters), replace the split logic with
line.Substring()calls to extract each column by character position (e.g.,line.Substring(0, 5).Trim()for the first column). - Want sorting? Add a
ColumnClickevent handler to let users click headers to sort rows. - For larger files, use
StreamReaderto read line-by-line instead ofReadAllLines—but since you only have 10 rows,ReadAllLinesis totally fine.
That’s all! This will give you a clean, organized view of your partition data just like the example software you saw.
内容的提问来源于stack exchange,提问作者Kaustubh Patange

