C# 使用命令行调用应用程序

标签 c# c++ command-line argv argc

在我的 C++ 应用程序中,它是一个 .exe,我有这个:

int main(int argc, char** argv){
--argc;
++argv;
if (argc != 1){
    throw std::exception("Bad command line.");
}

E.t.c

但我如何在我的 C# (WPF) 应用程序中调用它?我尝试了 System.Diagnostics.Process.Start("pathofapp.exe", "stringiwantedtouse"),但我得到了 Bad Command Line。我该怎么办?

最佳答案

你应该尝试像这样使用 c++ 程序:

int main ( int argc, char *argv[] )
{
    if (argc != 1){
        throw std::exception("Bad command line.");
    }
}

然后使用 argv[0] 访问它的第一个参数。

例如:

在 C++ 中:

int main ( int argc, char *argv[] )
{
    if (argc != 1){
        throw std::exception("Bad command line.");
    }else{
        std::cout << "Param 1: " << argv[0] << std::endl;
    }
}

在 C# 中:

System.Diagnostics.Process.Start("pathofapp.exe", "this");

输出:

Param 1: this

关于C# 使用命令行调用应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20594304/

相关文章:

c# - 如何滚动到列表框的底部?

c# - 隐藏 EPPlus 中的列不起作用

c++ - Bubblesort 由于某种原因无法正常工作

windows - 如何将依赖walker输出到控制台?

windows - 在unix中获取windows中bat文件的返回值?

shell - 如何将包含空格的文件名作为参数传递给命令行程序?

C# Interop Non-invocable 成员 'Microsoft.Office.Interop.Excel.Range.End' 不能像方法一样使用

c# - Winforms 本地化错误

c++ - 如何知道 GetPrivateProfileInt/String 失败的原因

c++ - Boost ASIO 网络时序问题