![](/img/trans.png)
[英]How to deploy a C# (ClickOnce?) project that is a wrapper on a command line .exe
[英]Wrapper for a Command Line Tool in C#
使用MSDN我得到了类为我的命令行工具编写包装器。
我现在面临一个问题,如果我通过带有参数的命令行执行exe,它可以完美运行而不会出现任何错误。
但是当我尝试从Wrapper传递参数时,它会崩溃程序。
我想知道我是否正确地通过了论证,如果我错了,请有人指出。 这是来自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
我有两个选择 -
1)请尝试在“管理员模式”下运行Visual Studio。 或者2)尝试实现这一点。 https://github.com/commandlineparser/commandline 。
“Command Line Parser Library为CLR应用程序提供了一个简洁的API,用于操作命令行参数和相关任务。它允许您显示具有高度自定义的帮助屏幕以及向用户报告语法错误的简单方法。无聊和重复编程的所有东西都站在库的肩膀上,让你专注于核心逻辑。这个库提供了自2005年以来不断更新的API的无忧命令行解析。“
对我来说很棒。
我感觉它不喜欢你路径中的空间。 看到这篇文章: C#如何将目录空白空间用于process.arguements?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.