You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#中Console.Clear用法、WPF获取CMD命令结果及函数完善问询

Hey there! Let's tackle your questions step by step and get that mediainfo function working properly.

1. Console.Clear() Usage & Capturing CMD Output in WPF

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 = true and UseShellExecute = false in ProcessStartInfo
  • 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)
2. Fixing the GetMovieData Function for Mediainfo

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.exe eliminates extra overhead and avoids issues with command parsing (like handling spaces in filenames).
  • Quoted Filename: Wrapping FileName in quotes ensures paths with spaces or special characters are processed correctly.
  • Error Output Capture: Reading StandardError helps 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.Invoke ensures we switch back to the UI thread to set t_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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:38:38