将控制台工具和包组合到一个.NET dll中

时间:2013-09-05 15:06:13

标签: c# .net console

有没有办法将控制台工具和打包组合成一个托管的.net DLL。然后我可以通过调用DLL中的函数来调用每个控制台工具的函数。

实施例。 (仅举例)

trim.exe
 Usage:trim.exe <input> <ouput>

copy.exe
 Usage:copy.exe <input> <ouput>

然后我就可以把它们称为像这样的函数

Utilities.Trim("input.txt","ouptut.txt");
Utilities.Copy("input.txt","ouptut.txt");

遗憾的是,我无法访问这些控制台工具的源代码。

2 个答案:

答案 0 :(得分:1)

是, 您正在寻找从Embedded Resource运行可执行文件。

Embedding an external executable inside a C# program会有所帮助。

答案 1 :(得分:1)

可能的解决方案是运行外部工具,如下所示:

string windowsVersion = Utilities.GetWindowsVersion(); 
//...
static class Utilities { // Just a sample of cmd.exe invocation
    public static string GetWindowsVersion() {
        using(Process versionTool = new Process()) {
            versionTool.StartInfo.FileName = "cmd.exe";
            versionTool.StartInfo.Arguments = "/c ver";
            versionTool.StartInfo.UseShellExecute = false;
            versionTool.StartInfo.RedirectStandardOutput = true;
            versionTool.Start();
            string output = versionTool.StandardOutput.ReadToEnd();
            versionTool.WaitForExit();
            return output.Trim();
        }
    }
}

您可以将这些可执行文件嵌入到您的程序中(作为资源),在运行时将这些工具提取到特定位置,然后按照上面的指定运行这些工具。

请同时查看以下帖子:process.start() embedded exe without extracting to file first c#