C#中的命令行工具封装器

6

使用MSDN,我得到了一个为我的命令行工具编写包装器的类。

现在我面临一个问题,如果我通过带参数的命令行执行exe文件,它可以完美地工作而没有任何错误。

但是当我尝试从包装器中传递参数时,程序会崩溃。

想知道我是否正确地传递了参数,如果我错了,有人能指出来吗?这是来自MSDN的LaunchEXE类。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace SPDB
{
    /// <summary>
    /// Class to run any external command line tool with arguments
    /// </summary>
    public class LaunchEXE
    {
        internal static string Run(string exeName, string argsLine, int timeoutSeconds)
        {
            StreamReader outputStream = StreamReader.Null;
            string output = "";
            bool success = false;

            try
            {
                Process newProcess = new Process();
                newProcess.StartInfo.FileName = exeName;
                newProcess.StartInfo.Arguments = argsLine;
                newProcess.StartInfo.UseShellExecute = false;
                newProcess.StartInfo.CreateNoWindow = true; //The command line is supressed to keep the process in the background
                newProcess.StartInfo.RedirectStandardOutput = true;
                newProcess.Start();
                if (0 == timeoutSeconds)
                {
                    outputStream = newProcess.StandardOutput;
                    output = outputStream.ReadToEnd();
                    newProcess.WaitForExit();
                }
                else
                {
                    success = newProcess.WaitForExit(timeoutSeconds * 1000);

                    if (success)
                    {
                        outputStream = newProcess.StandardOutput;
                        output = outputStream.ReadToEnd();
                    }
                    else
                    {
                        output = "Timed out at " + timeoutSeconds + " seconds waiting for " + exeName + " to exit.";
                    }
                }
            }
            catch (Exception e)
            {
                throw (new Exception("An error occurred running " + exeName + ".", e));
            }
            finally
            {
                outputStream.Close();
            }

            return "\t" + output;           

        }
    }
}

这是我从主程序(Form1.cs)传递参数的方式

private void button1_Click(object sender, EventArgs e)
        {
            string output;
            output = LaunchEXE.Run(@"C:\Program Files (x86)\MyFolder\MyConsole.exe", "/BACKUP C:\\MyBackupProfile.txt", 100);
            System.Windows.Forms.MessageBox.Show(output);
        }

命令行工具接受以下命令,并且可以完美运行:

C:\Program Files (x86)\MyFolder>MyConsole.exe /BACKUP C:\MyBackupProfile.txt


当你说“程序崩溃”时,你是否收到异常、错误消息或其他信息? - RQDQ
程序只是崩溃了,我有那个命令行工具的日志文件,但它不是由我构建的...我收到了该工具开发人员的回复,说这是我的问题,他的工具运行良好。但这就是我从工具的日志文件中得到的信息..."System.NullReferenceException: Object reference not set to an instance of an object." 然而,在我的程序中我没有收到任何异常错误... - Vivian Lobo
在 newProcess.Start(); 处设置断点会导致程序崩溃。 - Vivian Lobo
1
如果我是你,而且没有访问该工具的权限,我会制作一个虚拟exe文件,以确保在两种情况下都正确记录接收到的参数。 - Mahmoud Fayez
谢谢@MahmoudFayez,我会这样做的,我还是个编程新手..感谢你的提示。 - Vivian Lobo
显示剩余2条评论
2个回答

2
我有两个选项供您选择 - 1) 请尝试以“管理员模式”运行您的Visual Studio。 或者 2) 尝试使用此替代方案。Command Line Parser Library(命令行解析器库)提供了一个干净简洁的API,用于操作命令行参数及其相关任务。它允许您展示一个高度定制的帮助屏幕,并以简单的方式向用户报告语法错误。所有繁琐和重复的编程工作都由该库完成,让您可以集中精力处理核心逻辑。自2005年以来,该库提供了无麻烦的命令行解析及不断更新的API。在我这里效果非常好。 https://github.com/commandlineparser/commandline.

0

我也曾这样想过...但我尝试在其中使用转义字符仍然失败了。此外,我试图找出它是否与权限有关。因为即使是带参数执行cmd也无法执行,只有调用cmd才能工作。这可能是权限问题吗?不过还是谢谢@MatthewEvans提供的链接。 - Vivian Lobo
我有一台Windows 7 Home Premium电脑,并在其中安装了VS 2010。我是否需要在VS中设置某种权限才能访问系统上的文件夹/文件? - Vivian Lobo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接