【问题标题】:Executing a command in the command prompt using c# for a wpf application在命令提示符下使用 c# 为 wpf 应用程序执行命令
【发布时间】:2011-10-31 09:58:31
【问题描述】:

我目前正在构建一个 WPF 应用程序。我希望能够选择一个二进制文件,使用命令提示符命令行参数将其解码为 .csv 文件,在我的应用程序中编辑其值,然后使用解码工具将其解码回二进制文件。我唯一的部分'我坚持在命令提示符中输入我的命令行参数。我搜索了一些东西,但我只能找到有关如何从代码中打开命令提示符而不是如何执行命令的信息。

任何帮助将不胜感激。 谢谢!

【问题讨论】:

  • 有很多关于 SO 的骗局:如何在 C# 中的 shell 上执行命令? WPF 与这个问题没有任何关系。

标签: c# wpf command-line command-line-arguments command-prompt


【解决方案1】:

checkout Process 类,它是 .NET 框架的一部分 - 有关更多信息和一些示例代码,请参阅 its documentation at MSDN

编辑 - 根据评论:

启动 7zip 并读取 StdOut 的示例代码

using System;
using System.Diagnostics;
using System.IO;

class Program
{
    static void Main()
    {
    ProcessStartInfo start = new ProcessStartInfo();
    start.FileName = @"C:\7za.exe"; // Specify exe name.
    start.UseShellExecute = false;
    start.RedirectStandardOutput = true;

    using (Process process = Process.Start(start))
    {
        // Read in all the text from the process with the StreamReader.
        using (StreamReader reader = process.StandardOutput)
        {
        string result = reader.ReadToEnd();
        Console.Write(result);
        }
    }
    }
}

一些示例链接:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    相关资源
    最近更新 更多