C#中Console.Clear用法、WPF获取CMD命令结果及函数完善问询
Hey there! Let's tackle your questions step by step and get that mediainfo function working properly.
Console.Clear() Basics
Console.Clear() is a console application-specific method that wipes all content from the console window and moves the cursor back to the top-left corner. Using it is straightforward:
Console.Clear();
Important note: This won't work in a standard WPF application (since there's no console window attached). If you need to use it, you can set your WPF project's output type to "Console Application" in the project properties—but that will show a console window alongside your WPF UI, which might not be what you want.
Capturing CMD Command Results in WPF
To run a command-line tool and store its output in a variable (or display it in your WPF UI), use the System.Diagnostics.Process class with output redirection. The core steps are:
- Set
RedirectStandardOutput = trueandUseShellExecute = falseinProcessStartInfo - Launch the process, read its standard output (and error output, for debugging)
- Ensure any UI updates happen on the WPF UI thread (critical to avoid cross-thread exceptions)
Your existing code has a couple of gaps: it doesn't tell cmd.exe to execute the command and exit, doesn't properly read the full output, and doesn't handle UI thread safety. Here's a polished, working version—with explanations of key improvements:
using System; using System.Diagnostics; using System.Windows; public void GetMovieData(string FileName) { // Use direct mediainfo call instead of cmd.exe (more efficient, fewer edge cases) var processStartInfo = new ProcessStartInfo { FileName = "mediainfo", // Wrap filename in quotes to handle spaces/special characters Arguments = $"--Inform=file://custom.txt \"{FileName}\"", RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = true, // Hide the command window (clean for WPF) WorkingDirectory = Environment.CurrentDirectory }; using (var process = Process.Start(processStartInfo)) { // Read all output/error before waiting for exit to avoid deadlocks string output = process.StandardOutput.ReadToEnd(); string error = process.StandardError.ReadToEnd(); // Wait for mediainfo to finish processing process.WaitForExit(); // Log errors if any (helpful for debugging) if (!string.IsNullOrWhiteSpace(error)) { Debug.WriteLine($"Mediainfo Error: {error}"); } // Update WPF UI safely (must run on the UI thread) Application.Current.Dispatcher.Invoke(() => { t_Dane_tech.Text = output; }); } }
Key Improvements Explained:
- Direct Mediainfo Call: Skipping
cmd.exeeliminates extra overhead and avoids issues with command parsing (like handling spaces in filenames). - Quoted Filename: Wrapping
FileNamein quotes ensures paths with spaces or special characters are processed correctly. - Error Output Capture: Reading
StandardErrorhelps diagnose issues if mediainfo fails to process the file. - UI Thread Safety: WPF controls can only be updated from the thread they were created on—
Dispatcher.Invokeensures we switch back to the UI thread to sett_Dane_tech.Text.
If You Must Use cmd.exe:
If you have a specific reason to go through cmd.exe, add the /c flag (tells cmd to execute the command and exit) and adjust the arguments:
var processStartInfo = new ProcessStartInfo { FileName = "cmd.exe", Arguments = $"/c mediainfo --Inform=file://custom.txt \"{FileName}\"", // Rest of the settings remain the same as above };
内容的提问来源于stack exchange,提问作者Sorky

